testing - 在测试中访问参数

标签 testing elixir phoenix-framework ecto

我正在尝试测试当我有查询参数时,我会根据该参数返回正确的内容。

我试过:

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/

相关文章:

elixir - 重定向后 Flash 会保留,但常规分配不会

elixir - 如何在发送 json 响应时避免 phoenix 中的 json 解码?

ruby - 为程序编写测试文件

testing - 带有 mocha 和 chai 的 Spectron 不起作用

java - Mockito 验证仅在 AVD 运行时通过

python - 为什么我得到 TypeError "argument of type ' 类型'不可迭代'?

elixir - 如何键入转换解码JSON,就好像它来自数据库一样

elixir - 如何让 Hound 等待页面完全加载?

elixir - 无法访问主 elixir 项目中的依赖项配置

websocket - Phoenix Framework Channels - (ErlangError) erlang 错误 : [reason: :function_clause, mfa : {Phoenix. Endpoint.CowboyHandler