This project is archived and is in readonly mode.

#5567 ✓invalid
Robert Speicher

nil object error when speccing a helper method that calls content_for

Reported by Robert Speicher | September 7th, 2010 @ 05:36 AM

I'm in the process of upgrading a production app to Rails3 and ran into an odd error while updating my specs for rspec2. I have a helper method that uses content_for, and when running the spec for it, I get an error like this:

  1) page_title should not fail
     Failure/Error: page_title('A', 'B', 'C')
     You have a nil object when you didn't expect it!
     You might have expected an instance of ActiveRecord::Base.
     The error occurred while evaluating nil.[]
     # /Users/tsigo/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/actionpack-3.0.0/lib/action_view/helpers/capture_helper.rb:137:in `content_for'
     # ./app/helpers/application_helper.rb:3:in `page_title'
     # ./spec/helpers/application_helper_spec.rb:7
     # /Users/tsigo/.rvm/gems/ree-1.8.7-2010.02@rails3/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:239:in `inject'
  • rails 3.0.0 final
  • rspec and rspec-rails 2.0.0.beta.20

It seems this is caused by ActionView::Base not being initialized during the spec process, so @_content_for never gets its initial Hash value and throws the nil error when trying to append to it.

I'm not sure if this is actually a Rails bug or something I'm doing wrong inside the spec. I've created an example Rails app that duplicates the behavior at

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>