Is possible to test before Rails loaded?

Viewed 74
1 Answers

Easy. Just setup a bootstapper file for your test that does not load Rails.

# spec/barebones_helper.rb
RSpec.configure do |config|

end

# optionally load your dependencies:
# Bundler.require(:some_group)
# spec/lib/mygem_spec.rb
require 'barebones_helper'

In fact rspec-rails already generates two separate files*:

  • spec_helper.rb - just configures rspec.
  • rails_helper.rb requires spec_helper.rb and then boots Rails through require File.expand_path('../config/environment', __dir__).

Simply changing require 'rails_helper' to require 'spec_helper' will change the test to become executed outside rails.

However be aware that running the tests through Spring will generate strange results as it will keep Rails running in the background. If you're using a spring binstub you can disable spring with the DISABLE_SPRING env var.

Related