2017-08-23 74 views
2

我是新来的elixir凤凰有一些问题访问测试内嵌套元素。我测试控制器,并得到了迄今为​​止以下回应:访问与elixir中的地图列表中的嵌套元素

.[%{"attributes" => %{"first_name" => "Timmy 96", "last_name" => 
"Assistant"}, 
"id" => "bca58c53-7c6e-4281-9bc8-0c4616a30051", 
"relationships" => %{"avatar" => %{"data" => %{"id" => "011300fd-ca98-42b4- 
9561-f1cdc93d2d25", 
"type" => "pictures"}}}, "type" => "users"}] 

我使用了应答JSON-API格式和我的读取与用户数据属性:

user_attr = Enum.filter(includes, fn(item)-> 
    item["relationships"]["avatar"] != nil 
end) 
IO.inspect user_attr 
case Enum.fetch(user_attr ,0) do 
    {:ok, value} -> 
    assert value["attributes"]["first_name"] == user.first_name 
    assert value["attributes"]["last_name"] == user.last_name 
    {_} -> 
    assert false 
end 

我想缩短这个部分,不想使用案例,但不知道如何在不使用案例中的值部分的情况下获取user_attr的值。

我也想要关系的id - >头像 - >数据 - > ID与我插入之前的ID,但不知道如何访问此值。 id是之前我插入的图片的一部分,所以我想

assert XXX == picture.id 

但如何让XXX?

希望有人能帮助我。去年只有Java和C#,从来没有Ruby,现在我得到了不知何故变成灵药:/

谢谢。

回答

2

您可以使用get_in/2来做到这一点。

iex()> list 
[%{"attributes" => %{"first_name" => "Timmy 96", "last_name" => 
"Assistant"}, 
"id" => "bca58c53-7c6e-4281-9bc8-0c4616a30051", 
"relationships" => %{"avatar" => %{"data" => %{"id" => "011300fd-ca98- 
42b4-\n9561-f1cdc93d2d25", 
    "type" => "pictures"}}}, "type" => "users"}] 

iex()> [map] = list 
[%{"attributes" => %{"first_name" => "Timmy 96", "last_name" => 
"Assistant"}, 
"id" => "bca58c53-7c6e-4281-9bc8-0c4616a30051", 
"relationships" => %{"avatar" => %{"data" => %{"id" => "011300fd-ca98- 
42b4-\n9561-f1cdc93d2d25", 
    "type" => "pictures"}}}, "type" => "users"}] 


iex()> get_in map, ["attributes", "first_name"] 
"Timmy 96" 
iex()> get_in map, ["attributes", "last_name"] 
"Assistant" 
iex()> get_in map, ["relationships", "avatar", "data", "id"] 
"011300fd-ca98-42b4-\n9561-f1cdc93d2d25" 
+0

谢谢。两个答案都有意义,但我使用了你的解决方案。竖起大拇指:) – Sardoan

+1

@Sardoan,因为您发现解决方案很有帮助,您应该将此答案标记为正确。 – mudasobwa

3

你应该尝试使用更多的模式匹配。

# fixture data. 
user = %{first_name: "Timmy 96", last_name: "Assistant"} 
picture = %{id: "011300fd-ca98-42b4-\n9561-f1cdc93d2d25"} 
value = %{ 
     "attributes" => %{"first_name" => "Timmy 96", "last_name" => "Assistant"}, 
     "id" => "bca58c53-7c6e-4281-9bc8-0c4616a30051", 
     "relationships" => %{ 
     "avatar" => %{ 
      "data" => %{ 
      "id" => "011300fd-ca98-42b4-\n9561-f1cdc93d2d25", 
      "type" => "pictures", 
      }, 
     }, 
     }, 
     "type" => "users", 
    } 

assert %{"attributes" => attributes} = value 
# ensure the expected value match with actual value and than bind the attributes variable with actual attributes map. 
assert %{"first_name" => user.first_name, "last_name" => user.last_name} == attributes 

assert %{"relationships" => %{"avatar" => %{ "data" => avatar_data}}} = value 
assert %{"id" => picture.id, "type" => "pictures"} == avatar_data 

Elixir最强大的功能之一是通过=运算符(匹配运算符)进行模式匹配。

上面的例子说明我们可以使用match操作符声明期望值的数据结构与实际值匹配。

了解更多有关测试和模式匹配: https://semaphoreci.com/community/tutorials/introduction-to-testing-elixir-applications-with-exunit