断言中未使用的警告变量

warning variable unused in assert

我可以像这样在重定向后测试内容

assert "/url" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert html_response(conn, 200) ==  "Content after redirection"

但是我不能像这样向断言 url 添加参数变量:

id = 10
assert "/url/#{id}" = redir_path = redirected_to(conn, 302)

它抱怨:

cannot invoke remote function id/0 inside match

所以如果我在使用它之前像这样定义路径:

url_path = "/url/%{id}"
assert url_path = redir_path = redirected_to(conn, 302)

warning: variable "url_path" is unused

??未使用?它是否在断言中使用...

不确定如何消除该警告或如何解决这个问题。

代码类似于 Chris 在此处的回复:

https://elixirforum.com/t/following-redirection-in-controllertests/10084

warning: variable "url_path" is unused

??未使用?它是否在断言中使用...

不,不是。您的断言将匹配任何值,因为如果您在匹配项的 LHS 上有一个变量名称,它会匹配任何值,并且该变量会被分配 RHS 的值。

在:

foo = 10
assert foo = 20
IO.inspect foo

断言通过并且 foo 打印为 20

如果您只想断言该字符串等于 redirected_to/2 返回的值,您可以使用 ==:

assert "/url/#{id}" == redirected_to(conn, 302)