DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Fix For Nil Object Error In Rails Test Fixtures

01.11.2006
| 13268 views |
  • submit to reddit
        If you're seeing errors like this when you run Rails tests:

# NoMethodError: You have a nil object when you didn't expect it!

You might need to edit test/test_helper.rb to make sure use_instantiated_fixtures is true:

self.use_instantiated_fixtures = true

Prior to 1.0, Rails automatically created instance variables out of fixtures. So if you had a fixture record named "foo", you could access it in your test as "@foo". As of 1.0, the default is to disable that feature, which breaks a lot of existing code. Mike Clark <a href="http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting" title="Mike Clark's Weblog: Faster Testing with Rails 1.0">explains the change</a>.