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

Chris Hoffman bosshoff at gmail.com
Thu Apr 12 11:56:16 EDT 2007


I'm trying to get _level1 what it needs to avoid erroring out.  So,
since I am now replacing

render :partial => 'view_container/level1', :locals => { :bar => @bar }

with

render 'view_container/_level1'

I need some way to get the locals into the _level1 partial.  I cannot
seem to achieve this with

@controller.template.stub!(:bar).and_return(@bar)

Are you able?

-Chris

On 4/12/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> On 4/12/07, Chris Hoffman <bosshoff at gmail.com> wrote:
> > First off, thanks for taking the time to investigate this problem.
> > This group has been fantastic in this respect.
> >
> > The thing that I keep tripping over is the passing of locals.  I am
> > trying to do the following, as the blog post indicates:
> >
> > setup do
> >  #...
> >  @controller.template.stub!(:local_var).
> >     and_return { puts "got called"; @local_var_stub}
> > end
>
> I'm confused about your goal here. Let's say you have two partials:
> _level1 and _level2. If you're trying to specify that _level1 renders
> _level2 using a mock, you would do what's in Jake's post and my recent
> example in this thread. In this case, _level2 is never actually
> rendered because the mock intercepts the call.
>
> If you're trying to specify that _level2 interacts with a variable,
> then you should describe the behaviour of _level2 directly in its own
> example.
>
> Does this make sense? If I'm not getting it, please post the whole
> example so I can see everything you're trying to do.
>
> Thanks,
> David
>
> >
> > I put in the puts just to see if it's getting called, which it isn't.
> >
> > -Chris
> >
> > On 4/12/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> > > On 4/12/07, Chris Hoffman <bosshoff at gmail.com> wrote:
> > > > Hello,
> > > >   Well, that site ostensibly showcases just the syntax I need to pull
> > > > off what I'm looking for, but I am unable to get it working.  In fact,
> > > > I fail to see how stubbing out 'render' under @controller.template
> > > > could possibly work, given David's discourse above.
> > > >   Has anyone here actually tried the code in this post?
> > > >
> > > > -Chris
> > >
> > > I just got this working:
> > >
> > > #spec/views/thing/outer_partial_rhtml_spec.rb
> > > require File.dirname(__FILE__) + '/../../spec_helper'
> > > describe "/thing/_outer_partial" do
> > >   it "should render inner_partial" do
> > >     @controller.template.should_receive(:render).with(:partial =>
> > > "inner_partial")
> > >     @controller.template.should_receive(:render).with(:partial =>
> > > "other_inner_partial")
> > >     render 'thing/_outer_partial'
> > >   end
> > > end
> > >
> > > #app/views/thing/thing/_outer_partial.rhtml
> > > <%= render :partial => "inner_partial" %>
> > > <%= render :partial => "other_inner_partial" %>
> > >
> > > This works just fine. Is there something you're trying to do that this
> > > example is missing?
> > >
> > > >
> > > > On 4/12/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> > > > > 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
> > > > > end
> > > > >
> > > > > 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.
> > > > >
> > > > > David
> > > > >
> > > > >
> > > > > >
> > > > > > 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
> > > > > >
> > > > > _______________________________________________
> > > > > rspec-users mailing list
> > > > > rspec-users at rubyforge.org
> > > > > http://rubyforge.org/mailman/listinfo/rspec-users
> > > > >
> > > > _______________________________________________
> > > > rspec-users mailing list
> > > > rspec-users at rubyforge.org
> > > > http://rubyforge.org/mailman/listinfo/rspec-users
> > > >
> > > _______________________________________________
> > > rspec-users mailing list
> > > rspec-users at rubyforge.org
> > > http://rubyforge.org/mailman/listinfo/rspec-users
> > >
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list