168

I have test-unit installed and rspec installed (along with -core, -expectations, -mocks and -rails version 2.6.x). When I run the command rails new foo, it uses test-unit to generate the test stub files instead of rspec.

Is there an option where I can tell rails to use rspec to generate the tests instead?

aarona
  • 35,986
  • 41
  • 138
  • 186
  • A lot of these answers are about creating a rails app without minitest, then manually adding and installing the rspec gem. What would be more useful is an answer explaining how to include a flag to set it as default, or perhaps through a rails template? – alex Jul 05 '23 at 20:13

5 Answers5

278

The following should work:

at command line:

rails new MYAPP -T # The -T option tells rails not to include Test::Unit

in Gemfile:

gem 'rspec-rails'

at command line:

bundle install
rails g rspec:install
Zabba
  • 64,285
  • 47
  • 179
  • 207
  • I think some of those steps are out of order, no? This looks like this would work though if done in the right order. Please edit your answer and I will mark it as accepted. – aarona Jul 18 '11 at 05:13
  • 60
    You don't need the `generator.rb` initializer, this is taken care of by RSpec's railtie. All you need is `rspec-rails` within the `development` group of the `Gemfile` which is enough to require it for the generators. You also don't need to generate the app with `-T`, you can delete the `test` dir and it will work. – Ryan Bigg Jul 18 '11 at 05:14
  • 1
    Can you edit the code from g.test_framework = :rspec to g.test_framework :rspec – Deepak Lamichhane Jul 02 '13 at 10:23
  • 8
    @RyanBigg: with -T option we'll have `"rails/test_unit/railtie"` commented out in `application.rb` – JNN Sep 12 '13 at 20:16
  • 14
    When you add the `rspec-rails` gem, it's important to put it under test **and** development, or the generators won't use RSpec even if you follow the above steps. – februaryInk Jun 15 '15 at 15:16
  • @februaryInk's comment above ^^ is golden – sixty4bit Mar 20 '16 at 03:10
75

Create your new rails application as:

rails new <app_name> -T

Or remove your test directory from your existing application:

rm -rf test/

Make an entry in your Gemfile:

gem 'rspec-rails'

From the command line install the gem

$ bundle install

From the command line install rspec into your application:

$ rails g rspec:install

Now your rails application uses RSpec instead of test-unit.

Sayuj
  • 7,464
  • 13
  • 59
  • 76
9

Once you created your rails application with:

rails new <app_name> -T  # to exclude Test::Unit

Add the RSpec gem to your Gemfile in the following way:

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

In Command line write:

bundle install  # this will install the missing gems

Now you need to install RSpec by running:

rails generate rspec:install

This will generate the following files:

create  .rspec
create  spec
create  spec/spec_helper.rb
create  spec/rails_helper.rb

I strongly recommended to read through all spec_helper and rails_helper comments to get a good understanding of what each option does.

Once everything is set you can run all your tests with:

bundle exec rspec

You can read more about the recommended spec_helper and rails_helper configurations on https://kolosek.com/rails-rspec-setup.

Nesha Zoric
  • 6,218
  • 42
  • 34
4

I'm a new developer and I just made a rails flag (-rspec) to address OP's problem. It gets rid of Test::Unit and inserts the rails-rspec gem with a bash script. The script can be modified to help linux developers by automatically adding therubyracer gem or create custom flags and gemsets. (maybe specifically going to that gem line and deleting the comment)

Here's the gist && I hope this helps someone out there. https://gist.github.com/MTen/8310116

MTen
  • 186
  • 1
  • 4
1

There's no native way to do this with the rails CLI. Generate a new project without test-unit:

rails new rails-app --skip-test

And then follow rspec instructions to integrate it into the new Rails app.

thisismydesign
  • 21,553
  • 9
  • 123
  • 126