# Rails View spec testing for content in \<head\>

**URL:** <https://www.ruby-forum.com/t/rails-view-spec-testing-for-content-in-head/193622>\
**Category:** RSpec\
**Created:** [August 20, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/rails-view-spec-testing-for-content-in-head/193622 "2010-08-20T19:19:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [August 20, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/rails-view-spec-testing-for-content-in-head/193622/1 "2010-08-20T19:19:02Z")

</div>

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  
[#30 Pretty Page Title - RailsCasts](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:  
  
…

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [August 22, 2010, 8:52pm UTC](https://www.ruby-forum.com/t/rails-view-spec-testing-for-content-in-head/193622/2 "2010-08-22T20:52:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [August 23, 2010, 4:00pm UTC](https://www.ruby-forum.com/t/rails-view-spec-testing-for-content-in-head/193622/3 "2010-08-23T16:00:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [August 23, 2010, 4:41pm UTC](https://www.ruby-forum.com/t/rails-view-spec-testing-for-content-in-head/193622/4 "2010-08-23T16:41:16Z")

</div>

I found this at: [GitHub - rspec/rspec-rails: RSpec for Rails 5+](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?

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [August 23, 2010, 6:22pm UTC](https://www.ruby-forum.com/t/rails-view-spec-testing-for-content-in-head/193622/5 "2010-08-23T18:22:35Z")

</div>

Thank you David. Makes perfect sense.

-Michael

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [August 23, 2010, 4:28pm UTC](https://www.ruby-forum.com/t/rails-view-spec-testing-for-content-in-head/193622/6 "2010-08-23T16:28:58Z")

</div>

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
