[mocha-developer] Expecting calls with two different parameters

Bryan Helmkamp bhelmkamp at gmail.com
Mon Oct 9 09:41:28 EDT 2006


What I'm trying to do is mock the render call for my controller so
that the views never get processed when I run my functional tests, but
I can check that the correct view was going to be processed. The calls
to render (in this case) are burried in Rails' ActionPack code, since
it is using the default view for the action. Here's what I started
with for the mock:

@controller.expects(:render).with(:action => "edit")

 This doesn't work on its own because when using the default view,
Rails will call render once with no paramaters and once with the
action specified.  The error is:

Unexpected message :render() sent to #<Mocha::Mock: 30458920>
Similar expectations :render(:action => 'edit')

Basically, it's a fundamental aspect of the mocking behavior that's
causing the issue.  When a method is mocked for one call with one set
of parameters, it is implied that the method is to be called exactly
once with those exact parameters and no more.

It seems like this might be too strict.  On the other hand, to loosen
up this requirement would mean it would take two method calls to
achieve the current behavior:

@controller.expects(:render).with(:action => "edit")

I think it is interesting that the following won't work for my example:

@controller.expects(:render).with(:action => "edit")

It results in:

:render(:action => 'edit'): expected calls: 1, actual calls: 0

Even though the render method is called twice, and one of those calls
is with the :action => "edit" parameter.  It's like the first
expectation is blocking the second one from passing.



On 10/9/06, James Mead <jamesmead44 at gmail.com> wrote:
> On 08/10/06, Bryan Helmkamp <bhelmkamp at gmail.com> wrote:
> > Thanks, Kevin and James.  The key is instead of writing the mock like
> this:
> >
> > foo.expects(:bar)
> > foo.expects(:bar).with("something")
> >
> > You have to write it like this:
> >
> > foo.expects(:bar).with()
> > foo.expects(:bar).with("something")
> >
> > The first example won't work.  If you don't want to specify the
> > paramaters passed in one of the method calls, you can do:
> >
> > foo.expects(:bar).times(2)
> > foo.expects (:bar).with("something")
> >
> > And that works too.
> >
> > Thanks,
> >
> > -Bryan
> >
> I'm glad you've worked it out - I'm still not clear what the issue was. It
> would be clearer if you showed the actual method calls as well as the
> expectations.
>  I'm keen to observe the principle of least surprise, so please let me know
> if an aspect of the Mocha API didn't work how you expected.
> --
> James.
> http://blog.floehopper.org
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer

http://www.MyCongress.org/ -- coming soon

More information about the mocha-developer mailing list