Michael Kintzer
2010-Aug-20 17:16 UTC
[rspec-users] Rails View spec testing for content in <head>
Hi, Was trying to verify content in a title tag within a head tag using RSpec2/Rails3 and a view spec, but it seems that render/rendered API''s only return the html within the body tag. In my case the head tag is defined in a Rails layout file, with a yield :title, and the title tag content is set via a content_for :title section within the view, ala http://railscasts.com/episodes/30-pretty-page-title. Is there a way to validate head content in a view spec? Thanks, -Michael Example --- require "spec_helper" describe "users/sessions/new.html.erb" do describe "head" do it "should have a head" do render rendered.should have_selector("head") end end end Failure/Error: rendered.should have_selector("head") expected following output to contain a <head/> tag: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body>...
David Chelimsky
2010-Aug-22 18:25 UTC
[rspec-users] Rails View spec testing for content in <head>
On Aug 20, 2010, at 1:16 PM, Michael Kintzer wrote:> Hi, > Was trying to verify content in a title tag within a head tag using > RSpec2/Rails3 and a view spec, but it seems that render/rendered API''s > only return the html within the body tag. In my case the head tag is > defined in a Rails layout file, with a yield :title, and the title tag > content is set via a content_for :title section within the view, ala > http://railscasts.com/episodes/30-pretty-page-title. > > Is there a way to validate head content in a view spec? > > Thanks, > > -Michael > > Example > --- > require "spec_helper" > describe "users/sessions/new.html.erb" do > describe "head" do > it "should have a head" do > render > rendered.should have_selector("head") > end > end > end > > Failure/Error: rendered.should have_selector("head") > expected following output to contain a <head/> tag: > <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > "http://www.w3.org/TR/REC-html40/loose.dtd"> > <html><body>...The render method you see in a view spec delegates to the render method in ActionView::TestCase, which, in turn, delegates to view.render. Within rails, view.render is called usually by a controller, which provides all the necessary context information, like what layout to render. In this case, because you are specifying things that happen in a layout, you need to provide that context information in the spec: render :template => "users/sessions, :layout => "layouts/application" Note that you need to include ":template => ..." because RSpec''s render method only sets the template if there are no arguments. HTH, David> _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Kristian Mandrup
2010-Aug-23 13:54 UTC
[rspec-users] Rails View spec testing for content in <head>
I am trying to test methods I have added to ActionView::Base through a Rails 3 plugin. Basically, I have been trying to test it "outside of Rails" if possible, that is, to only load the bare minimum functionality. Here is my Rails view with a custom method #area added, which uses #with_output_buffer class MyView < ActionView::Base attr_accessor :current_user def area(clazz, &block) content = with_output_buffer(&block) content_tag :div, content, :class => clazz end end view = MyView.new require ''erb'' x = 42 template = ERB.new <<-EOF The value of x is: <%= my_area %> EOF puts template.result(binding) my_area = view.area :mine do ''hello'' end puts my_area my_area.should match /hello/ => <div class="mine"></div> Yeah, it outputs the content_tag without ''hello'' since it is output in a separate buffer. Bot sure how I would get around this!? And how to introduce this behavior into ERB? I guess I should read up on basic rspec view testing instead of trying to roll my own rspec framework for it! How would I do this using rspe-rails for RSpec 2? Thanks! Kristian
Kristian Mandrup
2010-Aug-23 14:14 UTC
[rspec-users] Rails View spec testing for content in <head>
I found this at: http://github.com/rspec/rspec-rails describe EventsHelper do describe "#link_to_event" do it "displays the title, and formatted date" do event = Event.new("Ruby Kaigi", Date.new(2010, 8, 27)) # helper is an instance of ActionView::Base configured with the # EventsHelper and all of Rails'' built-in helpers helper.link_to_event.should =~ /Ruby Kaigi, 27 Aug, 2010/ end end end So helper is an instance of ActionView::Base. Very useful... I think. But how do I use it for my scenario I wonder?
Kristian Mandrup
2010-Aug-23 14:28 UTC
[rspec-users] Rails View spec testing for content in <head>
I wonder if I can just "stub out" #with_output_buffer to yield the block immediately, and then assume it will work just as well in a Rails view template? class MyView < ActionView::Base attr_accessor :current_user def with_output_buffer yield end end
Michael Kintzer
2010-Aug-23 16:21 UTC
[rspec-users] Rails View spec testing for content in <head>
Thank you David. Makes perfect sense. -Michael> The render method you see in a view spec delegates to the render method in ActionView::TestCase, which, in turn, delegates to view.render. Within rails, view.render is called usually by a controller, which provides all the necessary context information, like what layout to render. In this case, because you are specifying things that happen in a layout, you need to provide that context information in the spec: > > ? render :template => "users/sessions, :layout => "layouts/application" > > Note that you need to include ":template => ..." because RSpec''s render method only sets the template if there are no arguments. > > HTH, > David >