specs: always require rails_helper

Test helpers are separated between two files: spec_helper and
rails_helper. This separation is meant to allow tests that do not
require Rails (like testing standalone libs) to boot faster.

The spec_helper file is always loaded, through `--require spec_helper`
in the `.rspec` config file. When needed, the rails_helper file is
expected to be required manually.

This is fine, but:
- Many test files have a redundant `require 'spec_helper'` line;
- Many test files should require `rails_helper`, but don't.

Not requiring `rails_helper` will cause the Rails-concerned section of
the test environment not to be configured–which may cause subtle bugs
(like the test database not being properly initialized).

Moreover, Spring loads all the Rails files on preloading anyway. So the
gains from using only `spec_helper` are thin.

To streamline this process, this commit:
- Configures `.rspec` to require `rails_helper` by default;
- Remove all manual requires to spec_helper or rails_helper.

Reference: https://stackoverflow.com/questions/24145329/how-is-spec-rails-helper-rb-different-from-spec-spec-helper-rb-do-i-need-it
This commit is contained in:
Pierre de La Morinerie 2020-03-25 15:04:48 +01:00
parent d6f6a076dd
commit 4cb747fdb6
161 changed files with 23 additions and 336 deletions

View file

@ -1,7 +1,11 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
# The generated `.rspec` file contains `--require rails_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../config/environment', __dir__)
require 'spec_helper'
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
@ -20,6 +24,8 @@ require 'rspec/rails'
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveSupport::Deprecation.silenced = true
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
@ -27,6 +33,11 @@ ActiveRecord::Migration.maintain_test_schema!
ActiveJob::Base.queue_adapter = :test
RSpec.configure do |config|
# Since rspec 3.8.0, bisect uses fork to improve bisection speed.
# This however fails as soon as we're running feature tests (which uses many processes).
# Default to the :shell bisect runner, so that bisecting over feature tests works.
config.bisect_runner = :shell
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
@ -49,4 +60,11 @@ RSpec.configure do |config|
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
config.infer_base_class_for_anonymous_controllers = false
config.include Shoulda::Matchers::ActiveRecord, type: :model
config.include Shoulda::Matchers::ActiveModel, type: :model
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view
end