1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

"Failure/Error: require 'rspec/rails' "と出たのでRSpecの設定を見直してみた

Posted at

はじめに

RSpecのテストを実行したら、下記のエラーが出ました。
require 'rspec/rails'は設定ファイルに明記しているのになぜ?

Failure/Error: require 'rspec/rails'

原因

rails_helper.rb の読み込み順序が誤っていました
当たり前ですが、上から順に設定ファイルの内容が読み込まれていきます
従って、下記に気を付ける必要があります。

・テスト環境の設定 ENV['RAILS_ENV']は、Rails環境が読み込まれる前に行う
・Rails環境 config/environmentは、RSpecの設定より先に読み込む
・RSpec関連の設定は、Rails環境が準備された後に行う


~修正前~

順序がぐちゃぐちゃですが載せておきます。。もちろんこれだと動きません。

rails_helper.rb

require_relative '../config/environment'
# rspec-rails を使用するための設定
require 'spec_helper'
# 本番や開発データベースを使用しないようにするための設定
ENV['RAILS_ENV'] ||= 'test'
# Rails の環境をテストモードに設定
require File.expand_path('../config/environment', __dir__)

# RSpec の実行環境が production になっていないかチェック
abort("The Rails environment is running in production mode!") if Rails.env.production?

require 'rspec/rails'

# (FactoryBot)spec/support ディレクトリ内のすべてのファイルを読み込む
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  abort e.to_s.strip
end
RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = Rails.root.join('spec/fixtures')
  # FactoryBot を使用するための設定
  config.include FactoryBot::Syntax::Methods
  # テストが DB を変更した場合にリセットする設定(RSpec の transactions を使用)
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  
  # 不要な `should` の警告を抑制
  config.expect_with :rspec do |expectations|
    expectations.syntax = :expect
  end
end

~修正後~

rails_helper.rb
# 本番や開発データベースを使用しないようにするための設定
ENV['RAILS_ENV'] ||= 'test'
# Rails の環境をテストモードに設定
require File.expand_path('../config/environment', __dir__)

require 'rspec/rails'
# rspec-rails を使用するための設定
require 'spec_helper'
require_relative '../config/environment'

# RSpec の実行環境が production になっていないかチェック
abort('The Rails environment is running in production mode!') if Rails.env.production?

# (FactoryBot)spec/support ディレクトリ内のすべてのファイルを読み込む
Dir[Rails.root.join('spec/support/**/*.rb')].sort.each { |f| require f }

begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  abort e.to_s.strip
end
RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = Rails.root.join('spec/fixtures')
  # FactoryBot を使用するための設定
  config.include FactoryBot::Syntax::Methods
  # テストが DB を変更した場合にリセットする設定(RSpec の transactions を使用)
  config.use_transactional_fixtures = true

  # テストが失敗した場合にスクリーンショットを撮る設定
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!

  # 不要な `should` の警告を抑制
  config.expect_with :rspec do |expectations|
    expectations.syntax = :expect
  end
end

おわりに

これまではたまたま正しい順序で記載されていたから動いてくれていたものの、設定を何かしら変更した影響で順序もおかしくなっていたようです。
THE初心者なミスですが、学びになりました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?