我正在尝试测试当我有查询参数时,我会根据该参数返回正确的内容。
我试过:
test "find_tags returns tips with the correct tag type" do
post = fixture(:post)
tip = Post |> Post.find_tags("tag", "connect"}) |> Repo.all
assert String.contains? tip.content, "#connect"
end
但是我得到一个错误。我不确定这是否是访问参数的正确方法,或者我是否遗漏了其他任何内容。
最佳答案
问题出在两个方面。最终的工作测试是:
test "find_tags returns tips with the correct tag type" do
post = fixture(:post)
tip = Post |> Post.find_tags(%{"tag" => "connect"}) |> Repo.all |> List.first
assert String.contains? tip.content, "#connect"
end
传入的参数需要格式为%{"tag"=> "connect"}
而不是{"tag", "connect"}
。这是在本地主机上访问页面时直接从终端中列出的参数中获取的。
另一个不起作用的部分是 tip
在使用 Repo.all
时是一个列表,因此可以通过 List 中的管道访问该结构。首先
。没有这个,tip.content
就不是字符串,并且存在参数错误。
关于testing - 在测试中访问参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42073003/