[rspec-users] Telling rspec that a method might be called with a OR b ... ?

Rick DeNatale rick.denatale at gmail.com
Tue Jul 28 17:38:32 EDT 2009

On Tue, Jul 28, 2009 at 4:57 PM, David Chelimsky<dchelimsky at gmail.com> wrote:
>> for arguments. Any way to tell rspec that a method might be called
>> with argument A or B (or C) ? :)
> There is no way to specify A or B, but you can stub both and set
> expectations about the resulting behavior that tie back to the stub
> values you set up. For example:
> @thing = stub_model(Thing)
> Thing.stub!(:new).and_return(@thing)
> Thing.stub!(:create).and_return(@thing)
> Thing.stub!(:create!).and_return(@thing)
> If you do that in a before(:each) block, now the implementation can
> use any of those methods and set expectations like
> assigns[:thing].should == @thing, etc. This effectively does what you
> are doing, but not in the most explicit way.

I think though that the OP want's to set a message expectation with
variation on the arguments, NOT which message.

Something like

   @some_object.should_receive(:some_message).with(any_of(a, b, c))

As long as you don't need to set different return values for different
arguments, that could be done with a new ArgumentMatcher.

Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

More information about the rspec-users mailing list