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

David Chelimsky dchelimsky at gmail.com
Tue Jul 28 16:57:56 EDT 2009


On Tue, Jul 28, 2009 at 2:28 PM, Marcelo de Moraes
Serpa<celoserpa at gmail.com> wrote:
> Hey list,
>
> Let's say I have a model, and I want to setup a partial mock in it, to
> set the expectation that it should receive a message with specific
> arguments. I know that this is not a good design for a spec, and that
> I would probably need to separate this in another example -- but let's
> say that I would like to express in the example that this partial mock
> could receive this message with agument a OR b. I had a situation
> where I have setup the should receive with a specific argument, but
> later on in the code flow, the class received the same message (find)
> with other arguments, which was breaking the spec. The null_object
> pattern would not work, because afaik, it is only for messages and not
> 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.

HTH,
David

>
> Thanks in advance,
>
> MArcelo.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list