spec / rails_helper.rb与spec / spec_helper.rb有何不同?我需要吗?


88

我第二次在做Rails教程。当我输入这个

rails generate integration_test static_pages

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

现在,当我运行测试时,它们比上次执行时更长(更“冗长”)并且更慢。我想知道两个文件之间的区别是什么,以及我做错了什么。另外,有没有一种方法可以摆脱rails_helper.rb文件而又不会弄乱一切?


您的测试产品以前没有生产过什么输出?(可能属于一个新问题。)
Dave Schweisguth 2014年

我不确定术语,但是现在测试通过了每一个步骤,这让我有了一长串我不了解的东西,然后结果才出现。以前,它只是给出结果。我会在这里复制它,但是它真的很长……
user3417583 2014年

可能是RSpec 3弃用。如果您无法通过搜索或myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3找出它们,请将它们放在一个新问题中。
Dave Schweisguth 2014年

1
它是固定的,我不得不从.rspec中删除--warnings
user3417583 2014年

Answers:


127

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.rbspec_helper.rb在您的规格中要求(而不是)。

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

如果您正在使用spring预加载器,则每个类仅需要加载一次,并且spring会急切地加载类,即使您只运行一个需要require的规范spec_helper,因此仅spec_helper在某些文件中没有太大价值。

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


4
这非常令人困惑。我要添加一个PR来更新rspec-rails自述文件,以将其拼写出来。感谢您的解释。
史蒂夫

4
对于从rspec开始的人们来说,这真是一团糟!
爱德华多'18年

1

您始终可以将所有配置组合到spec_helper中,只需要int rails helper文件中的spec helper。

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

#rails_helper.rb

require 'spec_helper'

#EMPTY FILE

并在其中引入所有特定于Rails的设置

# 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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.