[rspec-users] RSpec 2/Rails 3 - content_for in view specs

Dylan Markow dmarkow at gmail.com
Wed Aug 18 18:39:45 EDT 2010

My main layout includes separate content_for/yield sections for my header,
sidebar, footer, and content. However, when running a view spec, the
`rendered` variable seems to only contain a string of just the primary
content and ignores the header/footer/sidebar as well as the rest of my
layout file (the "render" call just returns my primary content wrapped in
generic <html> and <body> tags).

Is there a way to get to the content captured in a "content_for" block
through my view specs, or at the very least get my "render" call to use the
full layout?

I've tried doing a "render :layout => 'application'" which gives me:

1) contacts/show.html.erb shows a URL if the contact has one
    Failure/Error: render :layout => "application"
    undefined method `formats' for nil:NilClass
    # ./spec/views/contacts/show.html.erb_spec.rb:20:in `block (2 levels) in

I've also tried using response.capture(:sidebar), response[:sidebar],
rendered.capture(:sidebar), rendered[:sidebar], view.capture(:sidebar), etc.
and none of them worked -- they all give various errors.

Here is my view spec that I'm trying to run:

describe "contacts/show.html.erb" do
  it "shows a URL if present" do
    @contact = Factory(:complete_contact, :web_page => "
http://www.example.com") # Using Factory Girl
    rendered.should have_selector("span", :content => "

Thank you!

Dylan Markow
dmarkow at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100818/5ae5b44c/attachment.html>

More information about the rspec-users mailing list