[rspec-users] How to mock when there seems to be a requirement for chained mocked calls?

S Ahmed sahmed1020 at gmail.com
Mon Jun 13 22:44:44 EDT 2011


This is a method in my Model that I am writing a test for correct.

There are allot of if/else clauses in the method, and i want to make sure
certain things are called so I want to write expectations for it.

Not sure why you don't recommend such a thing? (chained expecations)


On Mon, Jun 13, 2011 at 9:37 PM, David Chelimsky <dchelimsky at gmail.com>wrote:

> On Jun 13, 2011, at 8:29 PM, S Ahmed wrote:
>
> "How to mock when there seems to be a requirement for chained mocked
> calls?"
>
> There is no such requirement unless you are imposing it by your own design
> decisions.
>
> > I want to mock the following:
> >
> > MyModel.where(".....").last
>
> Why do you want to do this? Is this in a model spec? A controller spec?
>
> > I tried:
> >
> > MyModel.should_receive(:where).and_return(nil)
> >
> > but this of course doesn't match the expectation since the call to .last
> was not mapped in the mock code.
> >
> > How can I do this?
>
> You _can_ stub (not mock) chains like this:
>
> MyModel.stub_chain(:where, :last).and_return(xxx)
>
> You can also set chained expectations like this (but I wouldn't recommend
> it):
>
> ar_query = double('ar_query')
> ar_query.should_receive(:last).and_return(nil)
> MyModel.stub(:where).and_return(ar_query)
>
> HTH,
> David
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110613/45f7c21c/attachment.html>


More information about the rspec-users mailing list