Search⌘ K
AI Features

JSON-based APIs

Understand how to write tests for JSON-based API endpoints in Phoenix using ExUnit. Explore testing controller update actions with happy path and error cases, and verify authentication through JSON Web Token validation plugs.

What will controller tests cover?

We strive to keep our controllers as small, with as little branching logic as possible. This accomplishes two goals:

  • As few test cases as possible.

  • Business logic is kept out of the web part of our application.

The logic for most controller actions can be tested with two tests: a happy path test and an error case test.

Note: There will always be exceptions to this, but it should be your starting goal.

Lastly, beyond testing the logic in the actions themselves, we may need to include tests to verify that certain plugs are included in our application’s call stack. For that, we’ll test for:

  • A JSON-based endpoint
...