Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

nil object error when speccing a helper method that calls content_for

#5567

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:

Failures:
  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 http://github.com/tsigo/content_for_bug

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

State: invalid
Milestone: none
Assigned to: Rohit Arondekar Rohit Arondekar
Importance: Low

Activity

  1. Robert Speicher
    Robert Speicher
    • Tag cleared.

    Please disregard, I figured out what I was doing wrong.

    September 7th, 2010 @ 05:47 AM

  2. David Trasbo
    David Trasbo
    • Assigned user set to Rohit Arondekar

    Can be closed, see above.

    September 7th, 2010 @ 02:34 PM

  3. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to invalid
    • Importance changed from to Low

    September 7th, 2010 @ 02:51 PM