在测试中访问参数

Accessing params in tests

我正在尝试测试当我有查询参数时,我 return 基于该参数的正确内容。

我试过:

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.first 中的管道访问该结构。没有这个,tip.content 就不是字符串,并且存在参数错误。