[rspec-users] [rspec] Stubbing partials in view specs

Zach Dennis zach.dennis at gmail.com
Wed Mar 18 18:23:28 EDT 2009


On Wed, Mar 18, 2009 at 6:18 AM, Evgeny Bogdanov
<evgeny.bogdanov at gmail.com> wrote:
> Hello,
> I have a problem similar to one mentioned in this post.
> I call a partial inside another partial.
> _mother.haml contains:
> render :partial => "children/child"
>
> In mother_spec.rb file I am trying to stub the render call.
> Here is a working version:
> =============
>    template.should_receive(:render)
>    template.stub!(:render)
>
>    render :partial => 'mother'
> ============
> I would prefer to specify that the partial I am stubbing is "children/
> child",
> however the following code doesn't work for me:
> ===========
>    template.stub!(:render).with(hash_including(:partial => 'children/
> child'))
>    template.expect_render(:partial => 'children/child')

What version of rspec are you using? #expect_render has been removed
in rspec 1.2 and has been deprecated for a while before that so you
won't want to rely on that unless you're using an old version of
rspec.

>
>    render :partial => 'mother'
>
> : Mock 'render_proxy' expected :render with ({:partial=>"children/
> child"}) once, but received it 0 times
> ===========
> Is it possible to stub the render call with specific partial name?

You had it right, hash including should work:
   template.stub!(:render).with(hash_including(:partial => "children/child"))

To stub all partials being rendered:
   template.stub!(:render).with(hash_including(:partial => anything))

HTH,


>
> Thank you in advance,
> Evgeny
>
> On Jan 20, 5:35 pm, Bart Zonneveld <zuperinfin... at gmail.com> wrote:
>> On 20-jan-2009, at 15:29, David Chelimsky wrote:
>>
>> > On Tue, Jan 20, 2009 at 7:44 AM, Bart Zonneveld
>> > <zuperinfin... at gmail.com> wrote:
>> >> Hey list,
>>
>> >> As a good BDDer I want to test my views in isolation.
>>
>> > Sort of. A *good* BDDer wants to *specify* views in isolation. Testing
>> > is for testers :)
>>
>> You're right! I tend to talk a lot to non-programmers, and they get
>> that glaze-in-the-distance look in their eyes, whenever I mention
>> specifiy, spec'ing, or what have you :).
>>
>> >> And as a good rails
>> >> programmer, I separate views into partials when needed. So, when
>> >> testing my
>> >> views, I want tostubout rendering of partials in my views. I'm
>> >> working on
>> >> upgrading an app from rails 2.1.2 to 2.2.2, using the latest rspec
>> >> and
>> >> rspec-rails.
>>
>> >> I used to throw template.stub!(:render) in a before(:each) block
>> >> and be done
>> >> with it
>>
>> > That sounds kinda risky because you could be ignoring partials that
>> > get rendered that you don't want to be rendered.
>>
>> It is, most definately.
>>
>> >> , but that doesn't work anymore. I can understand why, but now I have
>> >> to do something like template.stub!(:render).with(hash_including
>> >> (:partial =>
>> >> anything)). Except for when I'm testing a partial, then I need to
>> >> replace
>> >> the anything with every partial I'm rendering in my partial.
>>
>> >> Is this the correct way,
>>
>> > Seems like the only way at the moment. Wouldn't call it correct or
>> > incorrect.
>>
>> I would call it ugly :). Not only do I have to remember the
>> hash_including part, but also the anything (and not :anything).
>> Conceptually, I like the template.stub!(:render). Irendera
>> template, on which Istuball the renders. Whether that's risky or
>> not is a different discussion.
>>
>> >> or is there perhaps something like
>> >> template.stub_partials :only => [], :except => [] ?
>>
>> > Nothing like this exists. Seems like a reasonable idea. Feel free to
>> > submit a feature request, or better yet, a patch to
>> >http://rspec.lighthouseapp.com
>>
>> Will do!
>>
>> cheers,
>> bartz
>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>



-- 
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com


More information about the rspec-users mailing list