Search code examples
testingelixirphoenix-frameworkassert

warning variable unused in assert


I can test the content after a redirection like this

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

But I cannot add a param variable to the assert url like this:

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

It complains with:

cannot invoke remote function id/0 inside match

So if I just define the path like this before using it:

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

warning: variable "url_path" is unused

?? unusued? is it being used inside the assert...

Not sure how to silence that warning or how to approach this.

The code is similar to Chris's response here:

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


Solution

  • warning: variable "url_path" is unused

    ?? unusued? is it being used inside the assert...

    No, it's not. Your assert will match any value because if you have a variable name on the LHS of a match, it matches any value and that variable gets assigned the value of the RHS.

    In:

    foo = 10
    assert foo = 20
    IO.inspect foo
    

    the assert passes and foo is printed as 20.

    If you just want to assert that that string equals the value returned by redirected_to/2, you can use ==:

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