Jeremy Stephens
2007-Jun-29 21:17 UTC
[rspec-users] testing instance variables that are set inside views
Hi all, In my view specs (for Rails), how can I get at instance variables that are set within my view? For example, if my view looks like this: <% @header = "My Header" -%> <div>some content</div> How can I get to @header from within my view spec? I''ve tried @header and assigns[:header] to no avail. TIA, Jeremy -- Jeremy Stephens Computer Systems Analyst I School of Medicine Department of Biostatistics Vanderbilt University
aslak hellesoy
2007-Jun-30 07:52 UTC
[rspec-users] testing instance variables that are set inside views
@header is an implementation detail of your view that you shouldn''t care about in your view spec at all. I recommend you use the have_tag matcher instead to verify what content gets rendered as expected. Aslak On 6/29/07, Jeremy Stephens <jeremy.f.stephens at vanderbilt.edu> wrote:> Hi all, > > In my view specs (for Rails), how can I get at instance variables that > are set within my view? For example, if my view looks like this: > > <% @header = "My Header" -%> > <div>some content</div> > > How can I get to @header from within my view spec? I''ve tried @header > and assigns[:header] to no avail. > > TIA, > Jeremy > > -- > Jeremy Stephens Computer Systems Analyst I School of Medicine > Department of Biostatistics Vanderbilt University > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
barsalou
2007-Jun-30 20:35 UTC
[rspec-users] testing instance variables that are set inside views
Quoting aslak hellesoy <aslak.hellesoy at gmail.com>:> @header is an implementation detail of your view that you shouldn''t > care about in your view spec at all. I recommend you use the have_tag > matcher instead to verify what content gets rendered as expected. > > Aslak >Thanks Aslak, I wondered this myself. I looked around here : http://rspec.rubyforge.org/rdoc/index.html for have_tag but didn''t find it. Where would be a good place to find some of these matchers? Mike B. ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.
David Chelimsky
2007-Jun-30 20:41 UTC
[rspec-users] testing instance variables that are set inside views
On 6/30/07, barsalou <barjunk at attglobal.net> wrote:> Quoting aslak hellesoy <aslak.hellesoy at gmail.com>: > > > @header is an implementation detail of your view that you shouldn''t > > care about in your view spec at all. I recommend you use the have_tag > > matcher instead to verify what content gets rendered as expected. > > > > Aslak > > > > Thanks Aslak, I wondered this myself. I looked around here : > http://rspec.rubyforge.org/rdoc/index.html for have_tag but didn''t find > it. > > Where would be a good place to find some of these matchers?have_tag is not part of rspec''s core, it''s in the rails plugin. The rdoc for that is at http://rspec.rubyforge.org/rdoc-rails/index.html Cheers, David> > Mike B. > > ---------------------------------------------------------------- > This message was sent using IMP, the Internet Messaging Program. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
barsalou
2007-Jul-01 18:35 UTC
[rspec-users] testing instance variables that are set inside views
Quoting David Chelimsky <dchelimsky at gmail.com>:>>> >> Thanks Aslak, I wondered this myself. I looked around here : >> http://rspec.rubyforge.org/rdoc/index.html for have_tag but didn''t find >> it. >> >> Where would be a good place to find some of these matchers? > > have_tag is not part of rspec''s core, it''s in the rails plugin. The > rdoc for that is at http://rspec.rubyforge.org/rdoc-rails/index.html > > Cheers, > DavidPerfect, thanks! Mike B. ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.
Patrick Ritchie
2007-Jul-03 18:28 UTC
[rspec-users] testing instance variables that are set inside views
Hi, I think @header may not be an implementation detail in this case. If your layout looks like this: ... <head> <title><%= @header || ''Default Title'' %></title> </head> ... 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? Cheers! Patrick> @header is an implementation detail of your view that you shouldn''t > care about in your view spec at all. I recommend you use the have_tag > matcher instead to verify what content gets rendered as expected. > > Aslak > > On 6/29/07, Jeremy Stephens <jeremy.f.stephens at vanderbilt.edu> wrote: > >> Hi all, >> >> In my view specs (for Rails), how can I get at instance variables that >> are set within my view? For example, if my view looks like this: >> >> <% @header = "My Header" -%> >> <div>some content</div> >> >> How can I get to @header from within my view spec? I''ve tried @header >> and assigns[:header] to no avail. >> >> TIA, >> Jeremy >> >> -- >> Jeremy Stephens Computer Systems Analyst I School of Medicine >> Department of Biostatistics Vanderbilt University >> >> _______________________________________________ >> 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/20070703/7dc3b9d2/attachment-0001.html
aslak hellesoy
2007-Jul-04 07:54 UTC
[rspec-users] testing instance variables that are set inside views
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: > > ... > <head> > <title><%= @header || ''Default Title'' %></title> > </head> > ... > > 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. Aslak> Cheers! > Patrick > > > @header is an implementation detail of your view that you shouldn''t > care about in your view spec at all. I recommend you use the have_tag > matcher instead to verify what content gets rendered as expected. > > Aslak > > On 6/29/07, Jeremy Stephens > <jeremy.f.stephens at vanderbilt.edu> wrote: > > > Hi all, > > In my view specs (for Rails), how can I get at instance variables that > are set within my view? For example, if my view looks like this: > > <% @header = "My Header" -%> > <div>some content</div> > > How can I get to @header from within my view spec? I''ve tried @header > and assigns[:header] to no avail. > > TIA, > Jeremy > > -- > Jeremy Stephens Computer Systems Analyst I School of Medicine > Department of Biostatistics Vanderbilt University > > _______________________________________________ > 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 > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Don Petersen
2007-Jul-04 11:52 UTC
[rspec-users] testing instance variables that are set inside views
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
Patrick Ritchie
2007-Jul-04 15:35 UTC
[rspec-users] testing instance variables that are set inside views
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: >> >> ... >> <head> >> <title><%= @header || ''Default Title'' %></title> >> </head> >> ... >> >> 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 anywayThe 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... Cheers! Patrick -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070704/4b0e5cf3/attachment.html
David Chelimsky
2007-Jul-04 15:41 UTC
[rspec-users] testing instance variables that are set inside views
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: > > ... > <head> > <title><%= @header || ''Default Title'' %></title> > </head> > ... > > 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?> > Cheers! > Patrick > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Patrick Ritchie
2007-Jul-04 16:09 UTC
[rspec-users] testing instance variables that are set inside views
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: >> >> ... >> <head> >> <title><%= @header || ''Default Title'' %></title> >> </head> >> ... >> >> 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. Cheers! Patrick -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070704/624f3d80/attachment.html
Patrick Ritchie
2007-Jul-04 17:55 UTC
[rspec-users] testing instance variables that are set inside views
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
David Chelimsky
2007-Jul-04 18:02 UTC
[rspec-users] testing instance variables that are set inside views
On 7/4/07, Patrick Ritchie <pritchie at videotron.ca> wrote:> > 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?>From a design perspective, conventional wisdom suggests that you don''tput any decision making or control in your views. Doing this in helpers is the Rails Way.>From a practical perspective, there is no direct support for accessinginstance variables in views in rspec, so the helper route seems to be the path of least resistance. Of course, you could always use instance_eval. This IS Ruby, after all. But I wouldn''t recommend it. Cheers, David> > 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 > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >