Using Mocks to Simulate Database Failure

Learn how to simulate database failure using mocks.

Simulating database failure

A common use case for test doubles in Rails is to simulate database failures. Let’s look at how we can do that.

In the past, we would have suggested testing for failure at the controller level. However, Rails has basically deprecated controller tests for reasons we’ll talk about in Testing Rails Display Elements, and so we’d like to stay within the bounds of Rails community behavior.

Let’s look at the project-creation functionality we wrote at the beginning of the book. We wrote a controller:

Get hands-on with 1400+ tech skills courses.