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

Setting Constraints

Most of the test doubles you’ve created will accept any input. If you stub a method named jump with no other options, RSpec will use your stub whenever your code calls jump, jump(:with, :arguments), or jump { with_a_block }.

In this section, we’re going to look at ways to set constraints on a test double, so that RSpec only uses it if your code calls it in a certain way.

Constraining Arguments

In your projects, you’ll often want to check that your code is calling a method with the correct parameters. To constrain what arguments your mock object will accept, add a call to with to your message expectation:

 expect​(movie).to receive(​:record_review​).with(​'Great movie!' ...

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