[Rspec-users] Fwd: overriding mock expectations

David Chelimsky dchelimsky at gmail.com
Mon Oct 23 19:52:15 EDT 2006

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 ;)


>... 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")

specify "something" do

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


> Micah Martin
> 8th Light, Inc.
> www.8thlight.com

More information about the Rspec-users mailing list