I'm new to Phoenix and I'm testing a custom Plug that should redirect to the /404.html page if a condition is not met. The code works correctly when I try the success/failure paths but I'm having a difficult time understanding why my testing approach is blowing up.
The test failure boils down to the following:
%Plug.Conn{} |> Phoenix.Controller.redirect(to: "/404.html")
My expectation is that this should return a conn
object which I can then run assertions on. However, when I try to run the above code I get the following error:
** (UndefinedFunctionError) function Plug.Conn.send_resp/4 is undefined or private. Did you mean one of:
This is a bit weird since the Phoenix.Controller module implements a send_resp/4
function and it imports the Plug.Conn at the top of its definition.
Why is it ignoring that function and trying to hit Plug.Conn
directly? I'm not calling that private function, a public function is delegating to it, which should be kosher unless I've missed something obvious. Is there an easy solution to this problem or should I take another approach?
EDIT
Here is the full stack trace from iex:
%Plug.Conn{} |> Phoenix.Controller.redirect(to: "/404.html")
** (UndefinedFunctionError) function Plug.Conn.send_resp/4 is undefined or private. Did you mean one of:
* send_resp/1
* send_resp/3
(plug) Plug.Conn.send_resp(nil, 302, [{"content-type", "text/html; charset=utf-8"}, {"cache-control", "max-age=0, private, must-revalidate"}, {"location", "/404.html"}], "<html><body>You are being <a href=\"/404.html\">redirected</a>.</body></html>")
(plug) lib/plug/conn.ex:393: Plug.Conn.send_resp/1
The redirect
function's implementation can be found here:
https://github.com/phoenixframework/phoenix/blob/v1.2.3/lib/phoenix/controller.ex#L297
The send_resp
function I expect it to hit can be found here:
https://github.com/phoenixframework/phoenix/blob/v1.2.3/lib/phoenix/controller.ex#L748
Phoenix.ConnTest.build_conn()
instead of%Plug.Conn{}
for testing. – Dogbert