Marcelo de Moraes Serpa
2009-Jul-28 18:28 UTC
[rspec-users] Telling rspec that a method might be called with a OR b ... ?
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) ? :) Thanks in advance, MArcelo.
David Chelimsky
2009-Jul-28 20:57 UTC
[rspec-users] Telling rspec that a method might be called with a OR b ... ?
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 >
Rick DeNatale
2009-Jul-28 21:38 UTC
[rspec-users] Telling rspec that a method might be called with a OR b ... ?
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