Ruby on Rails: Switch from test_unit to rspec

Ruby on-RailsRspecGemTestunit

Ruby on-Rails Problem Overview


I'm going through a tutorial that has suggested using rspec, but I have already gone through a lot of default rails installation. I really don't want to have to redo the installation at all. Anyway, when I run

$ rails g integration_test named

I get

  invoke  test_unit
  create    test/integration/named_test.rb

When I run bundle, various rspec gems are listed, but test_unit is not. The tutorial seems to have rails invoke rspec instead of test_unit without doing anything additional. How do I get rails to use rspec with the integration test generator command?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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.

Solution 3 - Ruby on-Rails

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    

Solution 4 - Ruby on-Rails

Came across this issue today. application.rb has to be updated with:

config.generators do |g|
  g.test_framework :rspec
  g.integration_tool :rspec
end

Solution 5 - Ruby on-Rails

To use RSpec instead of default Test::Unit, run following command first

$ rails generate rspec:install

This command will create following folder/files

create  .rspec
create  spec
create  spec/spec_helper.rb

Now whenever you used generator to generate rails components like controller, model etc, it will create corresponding RSpecs.

Solution 6 - Ruby on-Rails

In config/application, add this code

 config.generators do |g|
       g.test_framework  :rspec
       g.integration_tool :rspec
 end

Solution 7 - Ruby on-Rails

1. when create new rails app, skip TestUnit framework, or it will generate test_unit directory.

$rails new your_app --skip-test-unit

2. add below code to your_app/config/application.rb file:

config.generators do |g|
  g.test_framework :rspec
end

3. add below code to your_app's Gemfile:

group :test, :development do
  gem 'rspec-rails'
end

save it, and run bundle install to install rspec gem

4. Initialize the spec/ directory

rails generate rspec:install

more details, please refer: https://github.com/rspec/rspec-rails

Solution 8 - Ruby on-Rails

What I found that I did that some of the other methods works still is to check my spelling....I had what @tovodeverett had grouping rspec-rails with :development and :test but spelt development incorrectly. That fixed my issue but I was generating tests with test_unit instead of rspec.

Solution 9 - Ruby on-Rails

$ rails g model Account
      invoke  active_record
      create    db/migrate/20140205052617_create_accounts.rb
      create    app/models/account.rb
      invoke    test_unit
      create      test/models/account_test.rb
      create      test/fixtures/accounts.yml
$ rails d model Account

Running script/rails generate rspec:install does not add rspec as default framework. Added below command in config/application.rb and then it works

config.generators do |g|
  g.test_framework :rspec
end
$ rails g model Account
      invoke  active_record
      create    db/migrate/20140205052957_create_accounts.rb
      create    app/models/account.rb
      invoke    rspec
      create      spec/models/account_spec.rb
$ rails -v
Rails 4.0.2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionExplosion PillsView Question on Stackoverflow
Solution 1 - Ruby on-RailsSpyrosView Answer on Stackoverflow
Solution 2 - Ruby on-RailstovodeverettView Answer on Stackoverflow
Solution 3 - Ruby on-RailsfontnoView Answer on Stackoverflow
Solution 4 - Ruby on-RailsiRoninView Answer on Stackoverflow
Solution 5 - Ruby on-RailsAmit PatelView Answer on Stackoverflow
Solution 6 - Ruby on-RailsAmrit DhunganaView Answer on Stackoverflow
Solution 7 - Ruby on-RailsxautjzdView Answer on Stackoverflow
Solution 8 - Ruby on-Railszigloo99View Answer on Stackoverflow
Solution 9 - Ruby on-RailsAbhishek BoseView Answer on Stackoverflow