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

user3417583 picture user3417583 · Jun 10, 2014 · Viewed 22.8k times · Source

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?

Answer

Dave Schweisguth picture Dave Schweisguth · Jun 10, 2014

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