0

I have a pretty simple code in the phoenix controller. It does some stuff and returns content depending on the format:

def delete(conn, _params) do
  # some stuff here

  if get_format(conn) == "json" do
    conn |> put_status(200) |> json(%{})
  else
    conn |> redirect(to: "/")
  end
end

It works properly, but I have a problem with testing it. I can't test html return. How can I do it? dispatch/5 doesn't have anything related to format.

Alex Antonov
  • 14,134
  • 7
  • 65
  • 142

1 Answers1

0

Format is defined via accept header for connection, not for get or whatever. For json & html formats it should be application/json or html/text, respectively.

You can use this conn in your tests:

conn = build_conn
  |> Plug.Conn.put_req_header("accept", "text/html")
mgarciaisaia
  • 14,521
  • 8
  • 57
  • 81
Alex Antonov
  • 14,134
  • 7
  • 65
  • 142
  • When one says “test,” readers are usually expect `assert` and/or `doctest`, or any other _test_, rather than simple assignment that just works always. – Aleksei Matiushkin Sep 19 '16 at 13:18