[rspec-users] testing instance variables that are set inside views
Patrick Ritchie
pritchie at videotron.ca
Wed Jul 4 13:55:20 EDT 2007
Hi Don,
Thanks for the tip! That looks like a great way to handle this case.
So what does everybody think, is this the definitive answer for setting
instance variables in your views? (use a helper) or are their cases
where we may still want to check instance variables in view specs?
Cheers!
Patrick
> I can sympathize with not wanting to just verify the final html
> output, even though it would probably be sufficient in this simple case.
>
> I'm a big fan of helpers for any logic in views, even simple stuff
> like this. If you had a couple of methods in your ApplicationHelper
> that handled setting and getting the page title value, that would be
> very easy to test.
>
> Approaching it this way, you can use the helper specs to verify that
> the actual set/get logic worked. You can even take that whole 'title
> = @header || "Default Title"' logic out of your layout if you wanted,
> and put it in the helper where you're only testing that logic and
> nothing more.
>
> So in your view spec, where you had previously been wanting to test
> if an instance variable had been set, now you just need to verify
> that the helper method was called. Like so:
> @controller.template.should_receive(:set_page_title)
>
> That's how I have done this very logic in the past, and it feels
> right to me.
>
> Alternately, if you don't want to use helpers, you might use
> "content_for"(http://api.rubyonrails.org/classes/ActionView/Helpers/
> CaptureHelper.html). It's probably overkill for just a basic string
> like this, but maybe you can rig it to work in this case.
>
> Don
>
> On Jul 4, 2007, at 11:09 AM, Patrick Ritchie wrote:
>
>
>> David Chelimsky wrote:
>>
>>> On 7/4/07, Patrick Ritchie <pritchie at videotron.ca> wrote:
>>>
>>>> aslak hellesoy wrote: On 7/3/07, Patrick Ritchie
>>>> <pritchie at videotron.ca> wrote: Hi, I think @header may not be an
>>>> implementation detail in this case. If your layout looks like
>>>> this: ... ... And the view we are testing looks like this: ... <%
>>>> = @header = 'Specific Title'> ... Then setting @header is an
>>>> essential behavior for the view spec. Or am I missing something?
>>>> Before I can answer about how to test this with RSpec, I need to
>>>> understand what's going on in the code. Isn't the head part of
>>>> the layout evaluated before the body? If that's the case, what's
>>>> the point of assigning the @header variable in the view? It
>>>> wouldn't change the title anyway The layout is evaluated after
>>>> the view so the above code is a useful way to include some piece
>>>> of data in the head without having to initialize it in the
>>>> controller. Very handy for things like head/title or view
>>>> specific onload js etc...
>>>>
>>> Correct me if I'm wrong, but it seems to me that the behavior
>>> you're interested in is that the right thing shows up in the
>>> title: response.should have_tag('title', 'whatever I am
>>> expecting') Am I missing something?
>>>
>> I could do that, but that's not the really behavior I'm interested
>> in for the view. I'd rather look at the title tag in my layout spec
>> and the instance variable in my view.
>>
>> It doesn't really make a difference in this simple case, but it
>> would if the title was inserted from a partial called from the
>> layout, or the instance variable was used in multiple places in the
>> layout.
>>
>> Cheers!
>> Patrick
>> _______________________________________________
>> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070704/ebf47d56/attachment.html
More information about the rspec-users
mailing list