Constructing Your Test Environment

People new to test doubles often ask how much behavior to fake. After all, if a test is too far removed from reality, it won’t give you a clear idea of how the code will run in the real world. We’ve definitely seen tests that have gone overboard with mocks, stubs, spies, and null objects. It can be difficult to know where to draw the line.

To cut through the confusion, we like to think of a test suite as a laboratory for carefully exercising code. Your test doubles are your scientific instruments. They help you create the environment needed for each experiment. You use them to control the factors you care about, and nothing else.

If you were performing a chemistry experiment, you’d probably care about the temperature ...

Get Effective Testing with RSpec 3 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.