Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 单元测试断言json响应包含一个列表_Unit Testing_Elixir_Phoenix Framework - Fatal编程技术网

Unit testing 单元测试断言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

在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(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

请记住,每个测试只有一个逻辑断言“()