Read more

Rspec: Complex argument expectations for should_receive

Tobias Kraze
August 25, 2014Software engineer at makandra GmbH

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'])
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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
Posted by Tobias Kraze to makandra dev (2014-08-25 10:42)