How can I tell Rails to use RSpec instead of test-unit when creating a new Rails app?

我是研究僧i 提交于 2020-01-11 14:50:09

问题


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?


回答1:


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



回答2:


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.




回答3:


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.




回答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



来源:https://stackoverflow.com/questions/6728618/how-can-i-tell-rails-to-use-rspec-instead-of-test-unit-when-creating-a-new-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!