首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >spec/rails_helper.rb与spec/spec_helper.rb有什么不同?我需要它吗?

spec/rails_helper.rb与spec/spec_helper.rb有什么不同?我需要它吗?
EN

Stack Overflow用户
提问于 2014-06-10 23:45:07
回答 2查看 26.4K关注 0票数 93

我正在做Rails教程的第二次。当我输入以下内容时

代码语言:javascript
复制
rails generate integration_test static_pages

我得到了spec/rails_helper.rbspec/spec_helper.rb,而不仅仅是spec/spec_helper.rb

现在,当我运行我的测试时,它们比我上次运行时更长(更“冗长”)和更慢。我想知道这两个文件之间的区别是什么,以及我是否做错了什么。另外,有没有一种方法可以在不弄乱一切的情况下删除rails_helper.rb文件?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-11 00:04:53

rspec-rails 3生成spec_helper.rbrails_helper.rbspec_helper.rb用于不依赖于Rails的规范(比如lib目录中的类的规范)。rails_helper.rb用于依赖于Rails的规范(在Rails项目中,大多数或全部)。rails_helper.rb需要spec_helper.rb。所以不,不要去掉rails_helper.rb;在你的规范中需要它(而不是spec_helper.rb)。

如果您希望非Rails依赖的规范强制它们不依赖于Rails,并且当您自己运行它们时尽可能快地运行,那么您可以在这些规范中使用spec_helper.rb而不是rails_helper.rb。但是在您的.rspec中使用-r rails_helper非常方便,而不需要在每个规范文件中使用一个或另一个帮助器,因此这肯定是一种流行的方法。

如果您使用的是spring预加载器,那么每个类只需要加载一次,并且是spring loads classes eagerly even if you only run a single spec that requires spec_helper,所以在某些文件中只需要spec_helper没有多大价值。

来源:https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files

票数 137
EN

Stack Overflow用户

发布于 2019-12-24 00:58:27

您可以随时将所有配置组合到spec_helper中,并且只需要rails助手文件中的spec helper。

这绝对不是“理想的”,因为在一天结束的时候,你是手动地做这个“重构”,但如果它真的困扰你的话。只需知道如何构建Rspec.configure完全取决于您

代码语言:javascript
复制
#rails_helper.rb

require 'spec_helper'

#EMPTY FILE

中引入所有rails特定的设置。

代码语言:javascript
复制
# spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'

require File.expand_path('../config/environment', __dir__)

# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  puts e.to_s.strip
  exit 1
end
RSpec.configure do |config|

... all our config.whatever_your_heart_desires
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24145329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档