...

/

Setup Block for Testing JSON-based APIs

Setup Block for Testing JSON-based APIs

Get familiarized with the basic structure and setup block for testing JSON-based APIs.

Testing the update endpoint

In the included repo, NotSkull, let’s create a new test file at test/not_skull_web/controllers/json_api/user_controller_test.exs.

Note: We’ve added nesting under json_api to avoid conflicting with the tests for the user controller that we used for the server-rendered HTML endpoint.

The following code will add a basic structure and setup block to the file we created:

Press + to interact
#file path -> test/not_skull_web/controllers/json_api/user_controller_test.exs
defmodule NotSkullWeb.JsonApi.UserControllerTest do
use NotSkullWeb.ConnCase, async: false
alias NotSkull.Accounts.User
describe "PUT /api/users/:id" do
setup context do
{:ok, user} = Factory.insert(:user)
conn_with_token =
put_req_header(
context.conn,
"authorization",
"Bearer " <> sign_jwt(user.id)
)
Map.merge(context, %{user: user, conn_with_token: conn_with_token})
end
end
end

A couple of things to note here:

  • When starting a new Phoenix project, the generators will create a few ...