Search⌘ K
AI Features

Simulating Requests

Explore how to simulate HTTP requests in Rails request tests to verify controller actions, handle user inputs, and test side effects like emails or background jobs. Understand the goals of request tests, avoid overlapping with model tests, and use Rails methods to mimic typical HTTP verbs for effective and isolated testing.

Ideally, our controllers are relatively simple. The complicated functionality is in a model or other object and tested in our unit tests for those objects. One reason this is a best practice is that models are easier to test than requests because they’re easier to extract and are used independently in a test framework.

Note: A request test should test the behavior of a single request. A request test should not fail because of problems in the model.

Request tests performance

A request test that overlaps with model behavior is part of the awkward middle ground of testing that the Rails 5 changes are designed to avoid. If the request test is going to the database, the test is slower than it needs to be. If a model failure can cascade into the controller tests, it’s harder than it ...