im writing phoenix application with ecto and have the following snippet in the test
{:ok, data} = Poison.encode(%{email: "nonexisting@user.com", password: "mypass"})
conn()
|> put_req_header("content-type", "application/json")
|> put_req_header("accept", "application/json")
|> post(session_path(@endpoint, :create), data)
> json_response(:not_found) == %{}
this throws a Ecto.NoResultsError
i have this defined
defimpl Plug.Exception, for: Ecto.NoResultsError do
def status(_exception), do: 404
end
but the test still throws Ecto.NoResultsError, Any pointers?
Phoenix 1.1.0 introduced
Phoenix.ConnTest.assert_error_sent/2
to make testing similar cases easier.From the documentation:
Usage example:
Let's consider how it works per environment.
In
:prod
, the default is to render error pages, so you should see a page rendered byYourApp.ErrorView
with the status code;In
:dev
, the default is to show debug pages, because the majority of times you have an error while building your code. If you want to see the actually rendered error page, you need to setdebug_errors: false
in yourconfig/dev.exs
;In
:test
, it works like production but, because you are calling your application from the test, your test will also crash if your application crashes. We are improving this on future versions, where you should be able to write something like: