[Rspec-users] overriding mock expectations

David Chelimsky dchelimsky at gmail.com
Tue Oct 24 05:41:08 EDT 2006


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
>


More information about the Rspec-users mailing list