Ruby on Rails: Switch from test_unit to rspec
Asked Answered
B

9

83

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?

Brow answered 27/3, 2012 at 5:53 Comment(0)
V
117

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
Verbenia answered 27/3, 2012 at 5:59 Comment(6)
I had already done that .. what do you mean by restart the server? It was never running.Brow
i mean restarting spork or your test interface. If you have properly done that and execute 'rails g scaffold --help' you should be seeing 'rspec' as the default testing framework. Check my edit as well.Verbenia
I updated the config/application.rb as you specified and ran rails g scaffold --help and it still says test_unit. Is there something I need to do to rerun the configuration or some way to manually change these settings?Brow
For future users wanting to go the other way, leaving this comment for completeness: Use the above answer with g.test_framework :test_unit .Lowman
how do I switch back? Can I use both generators at once?Stereotype
with the Rails app I'm testing on 6.1 (not sure which version this became true...) ... this method no longer works and gives me configuration.rb:97:in method_missing': undefined method test_framework' for #<Rails::Application::Configuration:0x00007fca9b924f10> (NoMethodError) ; For Rails 6-- see other answer about the gem being in the :test group but not the :development groupOchoa
S
73

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.

Scriptwriter answered 2/11, 2012 at 19:49 Comment(2)
+1 rspec-rails install as railitie github.com/rspec/rspec-rails/blob/master/lib/… and configures itself as test framework and integration tool. So one can include the gem in a shared group in the gemfile group :development, :test doFrida
Still relevant 7 years later!Gallard
P
32

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    
Pentomic answered 15/4, 2013 at 5:15 Comment(2)
This will works if the project is already created too. Just ignore the rails new command.Plumule
Watch out using the rm -Rf $RAILS_ROOT/test command, you'll remove your mailer previews too...Sloth
M
9

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
Marquismarquisate answered 19/7, 2012 at 8:20 Comment(1)
editing the application.rb file is no longer required, at least on Rails 3.2 with rspec-railsAbutting
C
8

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.

Charleton answered 28/3, 2012 at 12:17 Comment(2)
This will not work (rails 4) if you don't have rspec in your Gemfile already. See fontno's answer below.Mac
how do I switch back? Can I use both generators at once?Stereotype
S
3

In config/application, add this code

 config.generators do |g|
       g.test_framework  :rspec
       g.integration_tool :rspec
 end
Smithereens answered 18/7, 2013 at 9:13 Comment(0)
G
2

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

Groveman answered 23/8, 2014 at 13:38 Comment(0)
C
1

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.

Cranium answered 19/12, 2012 at 5:44 Comment(0)
M
1
$ 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
Markova answered 5/2, 2014 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.