Chapter 7Using Test Doubles as Mocks and Stubs

We have a problem. We want to add credit-card processing to our project application so that we can make money. Testing the credit-card functionality presents immediate difficulties. For one thing, we don’t want to accidentally make a credit-card purchase during testing—that would be bad. But even if the purchase gateway provides a test sandbox, we still don’t want to depend on it for our unit tests to run. That network call is slow and we don’t want our passing tests to depend on the status of a remote server.

We have a different problem. We’d like to build our code in a very modular kind of way. In doing so, we’d like our tests to be as isolated as possible from dependencies on other ...

Get Rails 4 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.