Ruby on Rails: Switch from test_unit to rspec
In your config/application.rb
file :
config.generators do |g|
g.test_framework :rspec
end
Now when you run your generators (example rails generate scaffold post
), you get rspec test files. Remember to restart your server. For more information on generators see:
RailsCasts #216 Generators in Rails 3
If you really want to use the integration_test generator you'll need to specifically modify the command:
rails g integration_test named --integration-tool=rspec
Working with Rails 3.2.8 and rspec-rails 2.11.4, I discovered that my problem was in my Gemfile. I had rspec-rails
in the :test
group but not the :development
group. Since Rails defaults to running in development mode (including when you're running generate), rspec-rails
has to be in your :development
group for it to hook into the generators. Once I had that in place, everything worked fine.
As of Rails 3.2.12, follow these steps in order
rails new app_name --skip-test-unit
Add rspec-rails to your Gemfile in the development, test group
group :development, :test do
gem 'rspec-rails'
end
Run bundle install
Run the generator
rails generate rspec:install
... and cleanup your existing test directory:
rm -Rf $RAILS_ROOT/test