简体   繁体   English

如何配置RSPEC永远不会在RAILS_ENV生产上运行

[英]How to configure RSPEC to never run on RAILS_ENV production

I was trying to run some test because of changes I did...so I ran RAILS_ENV=production bundle exec rspec 我试图运行一些测试,因为我做了更改...所以我运行了RAILS_ENV=production bundle exec rspec

which was something very stupid to do since rspec (the tests) were configured to truncate all the tables and this was exactly what happened on PRODUCTION !! 这是非常愚蠢的事情,因为rspec (测试)被配置为截断所有表格,这正是在PRODUCTION上发生的事情!! and you can imagine the consequences 你可以想象后果

Is there a way to configure rspec to never run when RAILS_ENV=production so this can never happen to any one. 是否有一种方法可以将RApec配置为在RAILS_ENV =生产时从不运行,因此任何一个都不会发生这种情况。

What other advice or good practices can be applied to avoid this kind of mistakes 可以采用哪些其他建议或良好做法来避免这种错误

UPDATE: I created a ISSUE for the rspec-rails team and they just commited a change that fixes this problem https://github.com/rspec/rspec-rails/pull/1383/files 更新:我为rspec-rails团队创建了一个ISSUE,他们只是提交了一个修复此问题的更改https://github.com/rspec/rspec-rails/pull/1383/files

In your Gemfile , refer to anything test-specific (like rspec-rails or rspec ) in the group for environments development and test only , instead of on the toplevel, eg: 在您的Gemfile ,请参阅组中的任何特定于测试的内容(如rspec-railsrspec ), 用于环境developmenttest ,而不是在顶层,例如:

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

Then bundling without the development and test gems on your production machine. 然后在生产机器上捆绑开发和测试宝石。 Without the rspec gem the tests would not have run. 没有rspec gem,测试就不会运行。 Add the --without switch when deploying: 部署时添加--without开关:

bundle install --without development test

在您的spec_helper.rb ,在任何RAILS_ENV分配之前:

raise "Not in test" unless ENV['RAILS_ENV'] == "test"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM