JSON-based APIs

Learn how to test a JSON based API.

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
  • A happy path
  • An error case
  • A presence of plug

Before we start writing tests, let’s look at the code we’ll test and how it fits into the application.

User controller

Our application is a simple game. It’s a pared-down version of a classic game called Skull and Roses.

Get hands-on with 1200+ tech skills courses.