How does rspec load Rails env?

17 views
Skip to first unread message

Dewey Garwood

unread,
Sep 2, 2018, 2:38:35 AM9/2/18
to rspec
Been using RSpec for a while (great testing tool!), and I'm trying to get another test framework to integrate with it (spinach-rails, specifically).

In rails 4.x, everything was working with the following Spinach config:

features/support/env.rb
  ENV["RAILS_ENV"] = "test"
  require './config/environment'
  require './spec/spec_helper.rb'
  ...

However, now when I import rails_helper.rb (in rails 5.2) in place of spec_helper.rb, I get an error indicating that it can't find the spec_helper file (required in the rails_helper file).  It seems clear to me that rails isn't autoloading the rest of the environment.

Rspec doesn't have these issues, and loads correctly, even when I remove the rspec-rails library from the rails_helper.rb.

Would you be able to help me understand how rspec/rspec-rails loads the rails env files and what role railties plays in that?  I was expecting to be able to use `require 'rails_helper'` and have everything else "just work".

Thank you for your help!

Dewey Garwood

unread,
Sep 2, 2018, 5:14:29 PM9/2/18
to rs...@googlegroups.com
Figured this out last night, with the help of some strategically placed `puts` statements.

Turns out that this piece of code here was the "secret sauce" that was allowing rspec to work: https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/ruby_project.rb#L8-L14

The spec/ and lib/ folders weren't being included by spinach in the $LOAD_PATH, and thus the reason the autoload wasn't working.

For any one trying to run spinach and tie it in with the rails_helper.rb in the future, here's what I had to do to get this working:

features/support/env.rb (spinach config file)
  # Help Spinach find the spec directory for autoloading
  $LOAD_PATH.unshift(::File.expand_path('../../spec', __dir__))
  require './config/environment'
  # Most of the desired config is already in rspec
  require 'rails_helper'
  ...

Thank you very much for allowing me to crash the rspec party!

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/940d9178-227a-42c9-8ea8-0b060d05a6c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages