Test API Endpoints

Learn how to test the API endpoints in Rails applications.

Testing major user flows

Just as we would test a major user flow, we should test major flows around our API. At the very least, each endpoint should have one test to make some assertions about the format of the response. While inadvertent changes to a UI can be annoying for users, such changes could be catastrophic for APIs. A test can help prevent this.

Our test should also use the authentication mechanism and content negotiation headers. Let’s write a complete set of tests for all this against our widget's endpoint.

The tests of the API should be integration tests, which means they should be in test/integration. To keep them separated from any normal integration tests we might write, we’ll use the same namespaces we used for the routes and controllers and place our test in test/integration/api/v1/widgets_test.rb.

Get hands-on with 1200+ tech skills courses.