2017-08-23 107 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