Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expect some (but not all) arguments with RSpec should_receive?

class Foo   def bar(a, b)   ...  Foo.should_receive( :bar ) 

expects bar to be called with any arguments.

Foo.should_receive( :bar ).with( :baz, :qux ) 

expects :baz and :qux to be passed in as the params.

How to expect the first param to equal :baz, and not care about the other params?

like image 237
B Seven Avatar asked Oct 07 '13 19:10

B Seven


2 Answers

Use the anything matcher:

Foo.should_receive(:bar).with(:baz, anything) 
like image 103
Dylan Markow Avatar answered Sep 29 '22 01:09

Dylan Markow


For Rspec 1.3 anything doesn't work when your method is receiving a hash as an argument, so please try with hash_including(:key => val):

Connectors::Scim::Preprocessors::Builder.     should_receive(:build).     with(       hash_including(:connector => connector)       ).     and_return(preprocessor) } 
like image 29
G. I. Joe Avatar answered Sep 29 '22 00:09

G. I. Joe