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

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.