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

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

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