Setup Block for Testing JSON-based APIs
Explore how to create a setup block for testing JSON-based APIs in Phoenix with ExUnit. Learn to configure ConnCase for HTTP testing, handle JWT headers, and manage the test sandbox environment to build effective API tests.
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_apito 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:
A couple of things to note here:
-
When starting a new Phoenix project, the generators will create a few different ExUnit test cases. One of them, ConnCase, or specifically
NotSkull.ConnCasein our app, will be used as the test case for all the HTTP-based interfaces. As seen on line 3, ...