[rspec-users] testing instance variables that are set inside views
pritchie at videotron.ca
Wed Jul 4 12:09:31 EDT 2007
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:
>> I think @header may not be an implementation detail in this case.
>> If your layout looks like this:
>> <title><%= @header || 'Default Title' %></title>
>> And the view we are testing looks like this:
>> <h1><%= @header = 'Specific Title'></h1>
>> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the rspec-users