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

2

101

I am doing the Rails Tutorial for the second time. When I enter this

rails generate integration_test static_pages

I get spec/rails_helper.rb and spec/spec_helper.rb instead of just spec/spec_helper.rb

Now when I run my tests, they are longer (more "verbose") and slower than when I did this last time. I am wondering what the difference between the two files is, and if I did something wrong. Also, is there a way to get rid of the rails_helper.rb file without messing everything up?

Paddle answered 10/6, 2014 at 15:45 Comment(4)
What output do your tests product that they didn't produce before? (Might belong in a new question.)Salon
I'm not sure about the terminology, but now the tests go through each gem which gives me a long list of things I don't understand, and only then does the result appear. Before, it just gave the result. I would copy it here but it's really long...Paddle
It's probably RSpec 3 deprecations. If you can't figure them out from searching or from this myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3, put them in a new question.Salon
it's fixed, I had to remove --warnings from .rspecPaddle
S
147

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

Salon answered 10/6, 2014 at 16:4 Comment(2)
This is very confusing. I'm gonna add a PR to update rspec-rails readme to spell it out as you have here. Thanks for the explanation.Interlingua
For people starting on rspec that is a big mess!Prothallus
L
1

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
Longley answered 23/12, 2019 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.