...
/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.
We'll cover the following...
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.exsdefmodule NotSkullWeb.JsonApi.UserControllerTest douse NotSkullWeb.ConnCase, async: falsealias NotSkull.Accounts.Userdescribe "PUT /api/users/:id" dosetup 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})endendend
A couple of things to note here:
-
When starting a new Phoenix project, the generators will create a few ...