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

David Chelimsky dchelimsky at gmail.com
Thu Mar 19 10:30:15 EDT 2009


On Mar 19, 2009, at 7:59 AM, Evgeny Bogdanov  
<evgeny.bogdanov at gmail.com> wrote:

> just upgraded to 1.2.0
> the following code works now:
>
>    template.stub!(:render).with(hash_including(:partial => "children/
> child"))
>    template.should_receive(:render).with(hash_including(:partial =>
> "children/child"))
>
> The only question that is left for me is about Nick's words:
> "If you set a method expectation on an object (IE:
> template.should_receive(:render) ), you don't need to stub the method
> (IE: template.stub!(:render) isn't needed)."
>
> Basically, there are two situation.
> 1) I want to check if a function was called and I want to execute the
> function code
> 2) I want to check if a function was called and I don't want to
> execute the function code so I used stubbing.
>
> I thought that for the first scenario the solution would be:
> template.should_receive(:render) #only checks if render function was
> called
>
> and for the second scenario
> template.stub!(:render) #stub render function
> template.should_receive(:render) #check if render function was called
>
> Am I right?

No. What I think you're describing is called a test spy: a means of  
monitoring interactions without changing the behavior. RSpec's mocks  
don't provide this. The only ruby framework I'm aware of that does is  
RR. There may be others.

A stub (stubs!) overrides an existing method (if one exists) and  
returns either self or any value you define with and_returns.

A message expectation, or mocked method (should_receive) does the same  
thing *plus* it verifies that the message was called.

HTH,
David

>
>
> Thanks,
> Evgeny
>
> On Mar 18, 11:23 pm, Zach Dennis <zach.den... at gmail.com> wrote:
>> On Wed, Mar 18, 2009 at 6:18 AM, Evgeny Bogdanov
>>
>>
>>
>> <evgeny.bogda... 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-us... at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
>> --
>> Zach Dennishttp://www.continuousthinking.comhttp://www.mutuallyhuman.com
>> _______________________________________________
>> 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


More information about the rspec-users mailing list