Hi,
I’m having trouble testing a JSON based API controller. It works fine in the browser, but all tests are throwing the same error.
I’ve created it using
mix phoenix.gen.json Api.Account accounts --no-model
The account model is working and it’s all tested.
Here’s my router.ex (only the relevant parts)
pipeline :api do
plug :accepts, ["json"]
end
...
scope "/api/v1", MyApp do
pipe_through :api
resources "/accounts", Api.AccountController, except: [:new, :edit]
end
Using my browser, I can go /api/v1/accounts and se the json being rendered correctly.
However, when I try to run my tests (generated by phoenix.gen.json), I get the following error in every single test:
mix test test/controllers/api/account_controller_test.exs
Output:
** (Phoenix.NotAcceptableError) no supported media type in accept header, expected one of ["html"]
And here’s a portion of my test script:
setup %{conn: conn} do
conn = conn |> put_req_header("accept", "application/json")
{:ok, conn: conn}
end
...
test "lists all entries on index", %{conn: conn} do
conn = get conn, account_path(conn, :index)
assert json_response(conn, 200)["data"] == []
end
What am I missing here?
Thanks in advance