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

我正在做的 Rails 教程的第二次。当我进入这个

rails generate integration_test static_pages

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

现在,当我运行测试时,它们比上次运行时更长(更“冗长”) ,也更慢。 我想知道这两个文件之间的区别是什么,如果我做错了什么。 还有,有没有办法在不搞砸一切的情况下删除 rails_helper.rb文件?

29778 次浏览

rspec-rails 3 generates spec_helper.rb and rails_helper.rb. spec_helper.rb is for specs which don't depend on Rails (such as specs for classes in the lib directory). rails_helper.rb is for specs which do depend on Rails (in a Rails project, most or all of them). rails_helper.rb requires spec_helper.rb. So no, don't get rid of rails_helper.rb; require it (and not spec_helper.rb) in your specs.

If you want your non-Rails-dependent specs to enforce that they're non-Rails-dependent, and to run as fast as possible when you run them by themselves, you could require spec_helper.rb rather than rails_helper.rb in those. But it's very convenient to -r rails_helper in your .rspec rather than requiring one helper or the other in each spec file, so that is sure to be a popular approach.

If you're using the spring preloader, each class only needs to be loaded once, and spring loads classes eagerly even if you only run a single spec that requires spec_helper, so there isn't as much value in requiring only spec_helper in some files.

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

You can always combine all your configs into the spec_helper and only require the spec helper int he rails helper file.

It is by no means "ideal" since at the end of the day, you are manually doing this "refactor" but IF it really bothers you. just know thats it totally up to you how to structure the Rspec.configure

#rails_helper.rb


require 'spec_helper'


#EMPTY FILE


and just bring in all the rails specific setup in

# 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