[rspec-users] Rails View spec testing for content in <head>

617 views
Skip to first unread message

Michael Kintzer

unread,
Aug 20, 2010, 1:16:52 PM8/20/10
to rspec...@rubyforge.org
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>...
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

David Chelimsky

unread,
Aug 22, 2010, 2:25:21 PM8/22/10
to rspec-users

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

Kristian Mandrup

unread,
Aug 23, 2010, 9:54:43 AM8/23/10
to rspec...@rubyforge.org
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

unread,
Aug 23, 2010, 10:28:27 AM8/23/10
to rspec...@rubyforge.org
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

Kristian Mandrup

unread,
Aug 23, 2010, 10:14:20 AM8/23/10
to rspec...@rubyforge.org
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?

Michael Kintzer

unread,
Aug 23, 2010, 12:21:48 PM8/23/10
to rspec...@rubyforge.org
Thank you David. Makes perfect sense.

-Michael

Reply all
Reply to author
Forward
0 new messages