Ruby on Rails:从 test_unit 切换到 rspec
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9884033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails: Switch from test_unit to rspec
提问by Explosion Pills
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
我正在阅读一个建议使用 的教程rspec,但我已经完成了很多默认的 rails 安装。我真的不想重新安装。不管怎样,当我跑
$ rails g integration_test named
I get
我得到
invoke test_unit
create test/integration/named_test.rb
When I run bundle, various rspecgems are listed, but test_unitis not. The tutorial seems to have rails invoke rspecinstead of test_unitwithout doing anything additional. How do I get rails to use rspecwith the integration test generator command?
当我运行时bundle,rspec会列出各种宝石,但test_unit不是。本教程似乎调用rspec了rails,而不是test_unit没有做任何额外的事情。如何让 railsrspec与集成测试生成器命令一起使用?
回答by Spyros
In your config/application.rbfile :
在您的config/application.rb文件中:
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:
现在,当您运行生成器(示例rails generate scaffold post)时,您将获得 rspec 测试文件。记得重启你的服务器。有关生成器的更多信息,请参阅:
RailsCasts #216 Generators in Rails 3
If you really want to use the integration_test generator you'll need to specifically modify the command:
如果你真的想使用 integration_test 生成器,你需要专门修改命令:
rails g integration_test named --integration-tool=rspec
回答by tovodeverett
Working with Rails 3.2.8and rspec-rails 2.11.4, I discovered that my problem was in my Gemfile. I had rspec-railsin the :testgroup but not the :developmentgroup. Since Rails defaults to running in development mode (including when you're running generate), rspec-railshas to be in your :developmentgroup for it to hook into the generators. Once I had that in place, everything worked fine.
在使用Rails 3.2.8和rspec-rails 2.11.4 时,我发现我的问题出在我的 Gemfile 中。我rspec-rails在:test组中但不在:development组中。由于 Rails 默认在开发模式下运行(包括运行 generate 时),rspec-rails因此必须在您的:development组中才能连接到生成器。一旦我有了它,一切都很好。
回答by fontno
As of Rails 3.2.12, follow these steps in order
从 Rails 3.2.12 开始,按顺序执行以下步骤
rails new app_name --skip-test-unit
Add rspec-rails to your Gemfile in the development, test group
将 rspec-rails 添加到开发、测试组中的 Gemfile
group :development, :test do
gem 'rspec-rails'
end
Run bundle install
跑 bundle install
Run the generator
运行生成器
rails generate rspec:install
... and cleanup your existing test directory:
...并清理您现有的测试目录:
rm -Rf $RAILS_ROOT/test
回答by iRonin
Came across this issue today. application.rb has to be updated with:
今天遇到这个问题。application.rb 必须更新为:
config.generators do |g|
g.test_framework :rspec
g.integration_tool :rspec
end
回答by Amit Patel
To use RSpec instead of default Test::Unit, run following command first
要使用 RSpec 而不是默认的 Test::Unit,请先运行以下命令
$ 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.
现在,每当您使用生成器生成控制器、模型等 Rails 组件时,它都会创建相应的 RSpec。
回答by Amrit Dhungana
In config/application, add this code
在配置/应用程序中,添加此代码
config.generators do |g|
g.test_framework :rspec
g.integration_tool :rspec
end
回答by xautjzd
1. when create new rails app, skip TestUnit framework, or it will generate test_unit directory.
1.新建rails app时,跳过TestUnit框架,否则会生成test_unit目录。
$rails new your_app --skip-test-unit
$rails new your_app --skip-test-unit
2. add below code to your_app/config/application.rb file:
2. 将以下代码添加到 your_app/config/application.rb 文件中:
config.generators do |g|
g.test_framework :rspec
end
config.generators do |g|
g.test_framework :rspec
end
3. add below code to your_app's Gemfile:
3. 将以下代码添加到 your_app 的 Gemfile 中:
group :test, :development do
gem 'rspec-rails'
end
save it, and run bundle installto install rspec gem
group :test, :development do
gem 'rspec-rails'
end
保存它,然后运行bundle install以安装 rspec gem
4. Initialize the spec/ directory
4.初始化spec/目录
rails generate rspec:install
rails generate rspec:install
more details, please refer: https://github.com/rspec/rspec-rails
更多详情请参考:https: //github.com/rspec/rspec-rails
回答by Abhishek Bose
$ 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:installdoes not add rspec as default framework. Added below command in config/application.rb and then it works
运行script/rails generate rspec:install不会将 rspec 添加为默认框架。在 config/application.rb 中添加以下命令,然后它就可以工作了
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
回答by zigloo99
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.
我发现我所做的一些其他方法仍然有效是检查我的拼写....我有@tovodeverett 将 rspec-rails 与 :development 和 :test 分组但拼写错误 development 的内容。这解决了我的问题,但我使用 test_unit 而不是 rspec 生成测试。

