There is one annoyance I''m encountering with the Mock API in rSpec. Overall it works well, as far as dynamic mocks go ;)... but there''s this one thing... It doesn''t allow overriding of expectations. example: m = mock("blah") m.should_receive(:one).any_number_of_times().and_return(1) m.should_receive(:one).and_return(1) The second call to should_receive is ignored. I believe it would be most convenient if the second call to should_receive would override the first. Why? What I would like to do is define all the should_receives once in the setup using any_number_of_times. This establishes a default context. Then in each spec I''d like to override a specific should_receive relevant to the spec. Without being able to override, I am force to define all the should_receives for each spec..... duplication. Is there another solution I''m missing? Micah Martin 8th Light, Inc. www.8thlight.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20061023/24f10a56/attachment.html
> What I would like to do is define all the should_receives once in > the setup using any_number_of_times. This establishes a default > context. Then in each spec I''d like to override a specific > should_receive relevant to the spec. > > Without being able to override, I am force to define all the > should_receives for each spec..... duplication. > > Is there another solution I''m missing?We need you to write a patch for this. If you don''t feel confident doing it, I''ll take a crack at it when I get home if you harass me on IM. srbaker0 yahoo stevenrbaker at mac.com .mac srbaker0 at hotmail.com msn -Steven>
This was intended for the list.... ---------- Forwarded message ---------- From: David Chelimsky <dchelimsky at gmail.com> Date: Oct 23, 2006 6:51 PM Subject: Re: [Rspec-users] overriding mock expectations To: Micah Martin <micah at 8thlight.com> On 10/23/06, Micah Martin <micah at 8thlight.com> wrote:> > There is one annoyance I''m encountering with the Mock API in rSpec. > Overall it works well, as far as dynamic mocks go ;)Hmmmmm.>... but there''s this one > thing... It doesn''t allow overriding of expectations. > > example: > > m = mock("blah") > m.should_receive(:one).any_number_of_times().and_return(1) > m.should_receive(:one).and_return(1) > > The second call to should_receive is ignored. I believe it would be most > convenient if the second call to should_receive would override the first. > > Why? > > What I would like to do is define all the should_receives once in the setup > using any_number_of_times. This establishes a default context. Then in > each spec I''d like to override a specific should_receive relevant to the > spec. > > Without being able to override, I am force to define all the should_receives > for each spec..... duplication. > > Is there another solution I''m missing?In 0.7 (currently in trunk - to be released within a couple of weeks - maybe even this week), you can do something close: setup do mock = mock("blah") mock.stub!(:msg).and_return(value) end specify "something" do mock.should_receive(:msg).and_return(other_value) end Would that solve your problem? If so, you can build it now from trunk, though you will have some changes you''ll need to to your existing specs. See the CHANGES file in the trunk root if you decide to do this. Cheers, David> > Micah Martin > 8th Light, Inc. > www.8thlight.com
On Oct 23, 2006, at 6:34 PM, Micah Martin wrote:> There is one annoyance I''m encountering with the Mock API in > rSpec. Overall it works well, as far as dynamic mocks go ;)... but > there''s this one thing... It doesn''t allow overriding of expectations. > > example: > > m = mock("blah") > m.should_receive(:one).any_number_of_times().and_return(1) > m.should_receive(:one).and_return(1) > > The second call to should_receive is ignored. I believe it would > be most convenient if the second call to should_receive would > override the first. > > Why? > > What I would like to do is define all the should_receives once in > the setup using any_number_of_times. This establishes a default > context. Then in each spec I''d like to override a specific > should_receive relevant to the spec. > > Without being able to override, I am force to define all the > should_receives for each spec..... duplication. > > Is there another solution I''m missing?I''m wondering if you should just move the specs that you want to override to another context, since the setup is different. Curious, Craig
Here''s a quick fix.... in mock.rb in the add method ... def add(expectation_class, expected_from, sym, &block) define_expected_method(sym) expectation = expectation_class.send(:new, @name, @expectation_ordering, expected_from, sym, block_given? ? block : nil) @expectations << expectation expectation end Line 47 could read: @expectations.insert(0, expectation) instead of @expectation << expectation As for a test...er spec.... specify "Mock expectations are overrideable" do m = mock("blah") m.should_not_receive(:one) m.should_receive(:one) m.one end Micah Martin 8th Light, Inc. www.8thlight.com On Oct 23, 2006, at 5:55 PM, Steven R. Baker wrote:>> What I would like to do is define all the should_receives once in >> the setup using any_number_of_times. This establishes a default >> context. Then in each spec I''d like to override a specific >> should_receive relevant to the spec. >> >> Without being able to override, I am force to define all the >> should_receives for each spec..... duplication. >> >> Is there another solution I''m missing? > > We need you to write a patch for this. > > If you don''t feel confident doing it, I''ll take a crack at it when > I get home if you harass me on IM. > > srbaker0 yahoo > stevenrbaker at mac.com .mac > srbaker0 at hotmail.com msn > > -Steven > > >> >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20061023/bd32afd3/attachment.html
David Chelimsky
2006-Oct-24 09:34 UTC
[Rspec-users] Fwd: Fwd: overriding mock expectations
again, intended for the list - hitting reply on these goes to the individual, not the list - i need to fix that. ---------- Forwarded message ---------- From: David Chelimsky <dchelimsky at gmail.com> Date: Oct 24, 2006 4:31 AM Subject: Re: [Rspec-users] Fwd: overriding mock expectations To: Micah Martin <micah at 8thlight.com> On 10/23/06, Micah Martin <micah at 8thlight.com> wrote:> On Oct 23, 2006, at 6:52 PM, David Chelimsky wrote: > > In 0.7 (currently in trunk - to be released within a couple of weeks - > > maybe even this week), you can do something close: > > > > setup do > > mock = mock("blah") > > mock.stub!(:msg).and_return(value) > > end > > > > specify "something" do > > mock.should_receive(:msg).and_return(other_value) > > end > > > > Would that solve your problem? > > Off the bat, it looks like that''d do. It seems like should_receive > (...).any_number_of_times is virtually the same as stubbing. Is that > correct?Yes, with one additional bit. Stub messages and mock expectations are stored separately. This allows the framework to first look for a mock expectation and then look for a stub if it can''t find a mock expectation. Under the hood, they are instances of the subtypes of the same class and the message expectation is actually should_receive(:msg).any_number_of_times, as you suggest.> > Micah >
On 10/23/06, Craig Demyanovich <demmer12 at fastmail.us> wrote:> On Oct 23, 2006, at 6:34 PM, Micah Martin wrote: > > > There is one annoyance I''m encountering with the Mock API in > > rSpec. Overall it works well, as far as dynamic mocks go ;)... but > > there''s this one thing... It doesn''t allow overriding of expectations. > > > > example: > > > > m = mock("blah") > > m.should_receive(:one).any_number_of_times().and_return(1) > > m.should_receive(:one).and_return(1) > > > > The second call to should_receive is ignored. I believe it would > > be most convenient if the second call to should_receive would > > override the first. > > > > Why? > > > > What I would like to do is define all the should_receives once in > > the setup using any_number_of_times. This establishes a default > > context. Then in each spec I''d like to override a specific > > should_receive relevant to the spec. > > > > Without being able to override, I am force to define all the > > should_receives for each spec..... duplication. > > > > Is there another solution I''m missing? > > I''m wondering if you should just move the specs that you want to > override to another context, since the setup is different.What Micah describes is a fairly common need. The idea is that the defaults are part of setup, but the overrides are part of a given spec. The stubbing feature coming up in 0.7 supports this very well. And using stub! in the setup and should_receive in the specs helps to clarify what is "interesting" in a given spec.> > Curious, > Craig > _______________________________________________ > Rspec-users mailing list > Rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >