O'Reilly logo

Effective Testing with RSpec 3 by Myron Marston, Ian Dees

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required