Skip to content
Latchkey

RSpec: An error occurred while loading rails_helper in CI

RSpec could not load spec/rails_helper.rb, which boots the Rails app. Because everything depends on that file, RSpec reports zero examples and prints the underlying error that broke the boot.

What this error means

The whole suite aborts with "An error occurred while loading ./spec/rails_helper.rb" and "Failure/Error: require File.expand_path", followed by the real exception. No examples run.

rspec
An error occurred while loading ./spec/rails_helper.rb.
Failure/Error: require File.expand_path('../config/environment', __dir__)

ActiveRecord::NoDatabaseError:
  We could not find your database: myapp_test

Common causes

Rails environment fails to boot

A database, missing constant, missing secret, or initializer error prevents config/environment from loading, and rails_helper requires it.

Missing setup in CI

The test database was not created/migrated, or a required ENV var/secret is unset on the runner.

Bad rails_helper customization

Code added to rails_helper (a require, a Capybara/driver setup) raises during load.

How to fix it

Fix the wrapped boot error

  1. Read the exception printed under the loading message; that is the real failure.
  2. Prepare the test database and set required secrets/ENV before running specs.
  3. If a require in rails_helper raises, fix or guard it.
Terminal
RAILS_ENV=test bin/rails db:prepare
bundle exec rspec

How to prevent it

  • Run db:prepare and set required secrets before specs.
  • Keep rails_helper requires minimal and resilient.
  • Reproduce CI boot locally with RAILS_ENV=test.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →