How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?

Solution 1:

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

Solution 2:

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