Problem
Explore how to use test doubles, also known as mocks and stubs, in Rails testing to isolate units and avoid external dependencies like network calls. Understand their role in creating reliable, fast tests by faking objects to simulate real components. This lesson helps you handle complex testing scenarios such as credit card processing without making real transactions, and supports modular design by enabling tests to run independently of unfinished or broken parts.
We'll cover the following...
First problem
We have a problem. We want to add credit card processing to the project application so that we can make money. Testing the credit card functionality presents immediate difficulties. For one, we don’t want to make a credit card purchase during testing accidentally because that would be bad. But even if the purchase gateway provides a test sandbox, we still don’t want to depend on it for the unit tests to run. That network call is slow, and we don’t want the passing tests to depend on a remote server’s status.
Second problem
Or we might ...