[rspec-users] Help to spec a rails helper method that uses content_for

Ben Aldred benaldred at gmail.com
Thu Jan 24 11:00:04 EST 2008

Hi guys,

I am a Rspec newbie and am having problems creating a spec for a RoR helper

I have in fact already implemented the helper method and am trying to create
retrospective specs. I know its the wrong way to do it but I am still
learning Rspec.

In my view I have a call to a helper method.

<% body_class "users" %>

which sets a CSS class declaration in the content_for block which is then
used in the <body> tag in application.html.erb layout.

  def body_class(class_name)
    content_for(:body_class) { " class=\"#{class_name}\"" }

I was trying the following in my view spec:

template.should_receive (:body_class).with("users").and_return('
render "/admin/users/index.html.erb", :layout => "application"
response.should have_tag('body.users')

I get a:

Expected at least 1 element matching "body.users", found 0.
<false> is not true.

when I printed the response.body to screen it does not contain he CSS class
declaration on the body tag, where as running mongrel and going to the page
everything is fine.

what am I doing wrong? I ended up doing:

render "/admin/users/index.html.erb", :layout => "application"
response[:body_class].should == ' class="users"'

I am interested to know why my first attempt did not work.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080124/86eb6565/attachment-0001.html 

More information about the rspec-users mailing list