Configuring Responses
Since a test double is meant to stand in for a real object, it needs to act like one. You need to be able to configure how it responds to the code calling it.
When you allow or expect a message on a test double without specifying how it responds, RSpec provides a simple implementation that just returns nil. Your test doubles will often need to do something more interesting: return a given value, raise an error, yield to a block, or throw a symbol. RSpec provides ways for your doubles to do each of these:
| allow(double).to receive(:a_message).and_return(a_return_value) |
| allow(double).to receive(:a_message).and_raise(AnException) |
| allow(double).to receive( ... |
Get Effective Testing with RSpec 3 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.