Forum: RSpec Correct way to spec partials

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
42791cadd55830917d960e1bc96490ad?d=identicon&s=25 Oleksandr Rudyk (Guest)
on 2008-10-24 03:09
(Received via mailing list)
Hi everybody,
1) Does anybody have full working example of how to test partial
templates?

2) What the correct place to test partials: controller or view spec? If
controller correct place,
should I use integrate_views?

3) Is it true that expect_render is deprecated and I have to
use should_receive(:render)

4) How to test that partial's locals hash is correct?

Thx
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-10-26 14:00
(Received via mailing list)
On Thu, Oct 23, 2008 at 8:07 PM, Oleksandr Rudyk <alex.rudyk@gmail.com>
wrote:
> Hi everybody,
> 1) Does anybody have full working example of how to test partial templates?
> 2) What the correct place to test partials: controller or view spec? If
> controller correct place,
> should I use integrate_views?

There are basically three options (from most granular to most coarse):

1. view examples rendering the partial directly
2. view examples rendering a template that includes the partial
3. controller examples with integrate_views

In practice, I don't think I ever go for #3, and the choice between #1
and #2 is largely context-dependent. In the end you want to (or
rather, I want you to ;) ) be equally comfortable with all three
approaches, understand the pros and cons of each, and make a decision
on a case by case basis.

> 3) Is it true that expect_render is deprecated and I have to
> use should_receive(:render)

Yes!

> 4) How to test that partial's locals hash is correct?

Do you mean that the partial gets the right hash? If so, that's
something you would expect from the including template:

describe "people/edit" do
  it "should render the _form partial" do
    assigns[:groups] = mock('groups')
    template.should_receive(:render).with(:partial => "form", :locals =>
{
      :groups => assigns[:groups]
    }
    render "people/edit"
  end
end

HTH,
David
Ad87b3869dc58ece9bc7b500067b340c?d=identicon&s=25 Jesse Clark (jesse-c)
on 2008-11-12 01:52
(Received via mailing list)
On Oct 26, 4:49 am, "David Chelimsky" <dchelim...@gmail.com> wrote:
> 2. view examples rendering a template that includes the partial
> 3. controller examples with integrate_views
>
> In practice, I don't think I ever go for #3, and the choice between #1
> and #2 is largely context-dependent. In the end you want to (or
> rather, I want you to ;) ) be equally comfortable with all three
> approaches, understand the pros and cons of each, and make a decision
> on a case by case basis.

Where can I find more information about how various contexts affect
the choice between #1 and #2?

I have a partial that is shared by two view templates and I only want
to test the partial once. So I created a new spec file with a describe
block for the partial. The partial is passed some locals and I would
like to test in my examples that these local variables are displayed
as expected. How would I go about setting these in the example so they
are available to the partial when it renders?

Thanks,
-Jesse
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2008-11-12 09:12
(Received via mailing list)
On 12 Nov 2008, at 00:51, Jesse Clark wrote:
>>
>> rather, I want you to ;) ) be equally comfortable with all three
> as expected. How would I go about setting these in the example so they
> are available to the partial when it renders?

What you can do is call the template from the example and set up a
stub for each of the locals. So if I have a partial like this:

     <%= name %> makes cheese for <%= friend_name %>

Then in the example, you can do this:

     describe "when there's a name and a friend name"
         before(:each) do
      template.stub!(:name).and_return("Mike")
             template.stub!(:friend_name).and_return("Susan")
         end
     end

Make sense?


In order to keep your view specs from becoming too brittle, I would
strongly suggest that you 'stub out' the rendering of the shared
partial in the specs for the two view templates that use the partial.
I usually do this by making a special helper method for rendering the
partial:

    <%= for relationship in relationships %>
        <%= render_relationship(relationship) %>
    <% end %>

This makes is easy to stub out the rendering in your main view
templates' specs:

     before(:each) do
         template.stub!(:render_relationship)
     end

Of course you'll really want a Cucumber test that makes sure the whole
stack fits together (locals are passed through with the correct names)
but I'd suggest that's a much more flexible solution than tying your
view specs together.

HTH,
Matt
Ad87b3869dc58ece9bc7b500067b340c?d=identicon&s=25 Jesse Clark (jesse-c)
on 2008-11-12 19:02
(Received via mailing list)
On Nov 12, 2008, at 12:11 AM, Matt Wynne wrote:

>>>> should I use integrate_views?
>>> and #2 is largely context-dependent. In the end you want to (or
>> describe
>
> Then in the example, you can do this:
>
>    describe "when there's a name and a friend name"
>        before(:each) do
>       template.stub!(:name).and_return("Mike")
>            template.stub!(:friend_name).and_return("Susan")
>        end
>    end
>
> Make sense?

Yep. Exactly what I was looking for. Thanks.

>
> than tying your view specs together.
>

I am stubbing out the rendering of the partial in the other view
specs. Although, I am just using: 'template.stub!(:render)' in
expectations describing other portions of the view and then I have an
expectation that uses 'should_receive' to make sure the partial is
getting called with the expected locals values.

Learning how to use Cucumber is on my list of things to do...
This topic is locked and can not be replied to.