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 tag:

On Aug 20, 2010, at 1:16 PM, Michael K. wrote:

Thanks,
rendered.should have_selector(“head”)
end
end
end

Failure/Error: rendered.should have_selector(“head”)
expected following output to contain a tag:

...

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

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/

=>

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

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?

Thank you David. Makes perfect sense.

-Michael

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs