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

Nick Hoffman nick at deadorange.com
Wed Mar 18 16:35:02 EDT 2009

Hey there Evgeny. My response is inline.

> 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'
> ============

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).

> 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')
>    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?

What I said above about method expectations and method stubs also
applies here. You should remove the call to #stub! .

However, I'd be inclined to use #should_receive rather than #expect_render :

template.should_receive(:render).with :partial => 'children/child'
render :partial => 'mother'

I haven't written many view specs though, so maybe we're supposed to
use #expect_render .

I hope that helps. Cheers,

More information about the rspec-users mailing list