Rspec: Complex argument expectations for should_receive

Updated . Posted . Visible to the public.

Sometimes you need complex expectations on method arguments like this

SomeApi.should_receive(:find).with(:query => '*foo*', :sort => 'timestamp ASC', :limit => 100).and_return(['some result'])

This is not very flexible, and failure messages will be hard to read.

Instead, consider doing this:

SomeApi.should_receive(:find) do |params|
  params[:query].should == '*foo*'
  params[:sort].should == 'timestamp ASC'
  params[:limit].should == 100
  
  ['some result']
end
Tobias Kraze
Last edit
Michael Leimstädtner
Keywords
object, method, call, invocation, method, return, value
License
Source code in this card is licensed under the MIT License.
Posted by Tobias Kraze to makandra dev (2014-08-25 08:42)