[rspec-users] Test if view renders appropriate partial?

David Chelimsky dchelimsky at gmail.com
Thu Apr 12 09:20:01 EDT 2007

On 4/12/07, Chris Hoffman <bosshoff at gmail.com> wrote:
> Hello,
>   I am testing out a partial that calls another, general purpose
> partial as part of its processing.  Is there a class I can mock in
> Rails views to accomplish what I need?  That is, could I do something
> like the following:
> SomeClass.should_receive(:render).with(:partial => "foo", :locals => {
> :bars => bars })

What you're interested in is the #render_partial instance method
defined in ActionView::Base.

The problem is that you need that method to work as expected the first
time (to render the outer partial) and mock only the second call (to
render the nested partial). There is no support for turning mock
methods on and off midstream, so you'd have to do something like this:

controller.template.should_receive(:render).with(#opts for outer partial) do
  #duplicate here what the method actually does in order to render the outer

controller.template.should_receive(:render).with(#opts for inner partial)

Of course you're duplicating rails code in your specs to do this,
which is bad, but I think it's the best option. Give a shot and let us
know how it works.


> I tried breakpointing the view, and it looks like I am greeted by an
> anonymous class, which I am unsure how to mock.  Thanks for the help.
> -Chris
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list