Testing the Happy Path
Explore how to write a happy path test for a Phoenix controller using ExUnit. Understand structuring tests that verify HTTP 200 responses, JSON output, and database side effects to ensure correct application behavior.
We'll cover the following...
We'll cover the following...
Defining the happy path test
The test is structured very similarly to the error test from the previous lesson. The common elements are these:
-
It accepts a context with a
Plug.Connthat contains a valid JWT. -
It calls the endpoint using
put/3, a helper from the Phoenix library. -
It uses
json_response/2to assert on the HTTP response code and to deserialize the JSON response. -
It makes assertions on the return value.
-
It makes assertions on the side effects.
Let’s add the following test inside the describe block PUT /api/users/:id.
...