Thursday, January 01, 2009

Avoiding the undefined method `use_transactional_fixtures=' error.

After adding rspec to my project and generating a model with rspec_model, I tried to run the generated spec test:
$ spec -cfprofile spec/
.../toshi/spec/spec_helper.rb:12: undefined method `use_transactional_fixtures=' for # (NoMethodError)
 from /usr/lib/ruby/gems/1.8/gems/rspec-1.1.11/lib/spec/runner.rb:184:in `configure'
 from /home/medined/Workspaces/toshi/spec/spec_helper.rb:8
 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
 from /home/medined/Workspaces/toshi/spec/models/book_spec.rb:1
After a fruitless internet search, I tried using a rake task:
$ rake spec
.
Finished in 0.061996 seconds
1 example, 0 failures

3 comments:

flee61255 said...

I ran into the same initial problem but unhappily, 'rake spec' gave the same very same error. I installed rspec and rspec-rails as gems in WinXP, doing a 'gem install rspec' followed by 'gem install rspec-rails'. This loaded version 1.1.12 for both gems, but apparently the order of gem installation is critical; running 'ruby script/generate rspec' results in the wrong file spec/spec_helper.rb being installed; it's the one found in the rspec gem directory, rather than the one from the rspec-rails gem directory. I unistalled both gems, then did a 'gem install rspec-rails', allowing the installer to automatically install the rspec gem as part of the process. Once this completed, I created a new project, ran 'ruby script/generate rspec', and verified that the correct spec/spec_helper.rb file was now present in my project directory. Both the spec and rake commands now work without errors.

Jason FB said...

this happens when you generate your rails app before 2.3 and then upgrade to a Rails version > 2.3.

The fix is to go to test/test_helper.rb and change:
class Test::Unit::testCase

to:
class ActiveSupport::TestCase

http://www.datatravels.com/technotes/2009/04/21/rails-error-undefined-method-use_transactional_fix/

Thomas said...

@Jason FB Thanks a lot. This was precisely the problem!