Unit testing 单元测试断言json响应包含一个列表
在phoenix框架中编写单元测试时,如何检查json响应是否包含列表 现有测试低于此值,该测试失败,因为Unit testing 单元测试断言json响应包含一个列表,unit-testing,elixir,phoenix-framework,Unit Testing,Elixir,Phoenix Framework,在phoenix框架中编写单元测试时,如何检查json响应是否包含列表 现有测试低于此值,该测试失败,因为子项已填充。我只想让测试告诉我,我的json响应包含子项,而子项是一个列表 test "shows chosen resource", %{conn: conn} do parent = Repo.insert! %Parent{} conn = get conn, parent_path(conn, :show, parent) assert json_response(con
子项
已填充。我只想让测试告诉我,我的json响应包含子项
,而子项
是一个列表
test "shows chosen resource", %{conn: conn} do
parent = Repo.insert! %Parent{}
conn = get conn, parent_path(conn, :show, parent)
assert json_response(conn, 200)["data"] == %{"id" => parent.id,
"children" => []}
end
为此,我将使用三个断言,首先使用模式匹配断言来断言基本结构,并提取
id
和子级
:
assert %{"id" => id, "children" => children} = json_response(conn, 200)["data"]
assert id == parent.id
assert is_list(children)
注意,即使映射包含除
id
和children
之外的键,此测试也将通过。我将使用三个断言,首先使用模式匹配断言断言基本结构并提取id
和children
:
assert %{"id" => id, "children" => children} = json_response(conn, 200)["data"]
assert id == parent.id
assert is_list(children)
请注意,即使映射包含的键不是
id
和children
With[json schema][2],此测试也将通过。您可以生成一个json以与()一起使用,以验证完整的json结构
iex> schema = %{
"type" => "object",
"properties" => %{
"foo" => %{
"type" => "string"
}
}
} |> ExJsonSchema.Schema.resolve
及
请记住,对于[json模式][2],每个测试只有一个逻辑断言“(),您可以生成一个json,用于()验证完整的json结构
iex> schema = %{
"type" => "object",
"properties" => %{
"foo" => %{
"type" => "string"
}
}
} |> ExJsonSchema.Schema.resolve
及
请记住,每个测试只有一个逻辑断言“()