web-dev-qa-db-ja.com

メッセージ:失敗/エラー:File.expand_path( '../../ config / environment'、__FILE__)が必要

最初に、同様の問題を持つユーザーの他の投稿を読みましたが、私の間違いがどこにあるのかわかりませんでした。次のファイルでRSpecを使用してテストを開始したいと思いました。

dashboard_view_spec.rb:

require 'Rails_helper'

RSpec.feature "Dashboard", type: :feature do 
before(:each) do
@current_user = User.create!(email: "[email protected]", password: "xyz123")
sign_in_with(@current_user.email,@current_user.password)
end

#NAV BAR RSPEC TEST
scenario 'Home bar nav link present' do
visit "/"
expect(page).to have_text('Home')
end

scenario 'How it work nav bar link present' do
visit "/"
expect(page).to have_text('How it work')
end

scenario 'Support nav bar link present' do
visit "/"
expect(page).to have_text('Support')
end

end

上部のRails_helper.rbで:

# This file is copied to spec/ when you run 'Rails generate rspec:install'
ENV['Rails_ENV'] ||= 'test'

require File.expand_path('../../config/environment', __FILE__)
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if   Rails.env.production?

require 'rake'
require 'spec_helper'
require 'rspec/Rails'
require 'shoulda/matchers'
require 'capybara/Rails'
require 'capybara/rspec'
require 'rspec/retry'
require 'devise'

エラーメッセージ:

Failure/Error: require File.expand_path('../../config/environment', __FILE__)

NoMethodError:
undefined method `[]' for nil:NilClass
# ./config/initializers/devise.rb:258:in `block in <top (required)>'
# ./config/initializers/devise.rb:5:in `<top (required)>'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/Rails_helper.rb:4:in `require'
# ./spec/Rails_helper.rb:4:in `<top (required)>'
# ./spec/views/dashboard_view_spec.rb:1:in `require'
# ./spec/views/dashboard_view_spec.rb:1:in `<top (required)>'
No examples found.

Randomized with seed 14549

次に、端末で使用したコマンド

 bundle exec rspec spec/views/dashboard_view_spec.rb

Deviseでのテストのドキュメントを確認した後、iはdashboard_view_spec.rbのコードを変更し、sign_inとして使用ユーザーと同じエラーメッセージが表示されました。

Devise.rbの258行目

 config.omniauth :facebook, Rails.application.secrets.facebook[:key],  Rails.application.secrets.facebook[:secret], scope: 'email', info_fields: 'email, name'

Gemfile内

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger     console
 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
 # Adds support for Capybara system testing and Selenium driver
 gem 'capybara', '~> 2.13'
 gem 'Selenium-webdriver'
 gem 'rspec-Rails', '~> 3.8'
 gem 'factory_girl_Rails'
 gem 'faker'
 gem 'database_cleaner'
end

そして

group :development do
# Access an IRB console on exception pages or by using <%= console %>    anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the  background. Read more: https://github.com/Rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
5
James Rich

同じエラーが発生しましたが、実行するとbundle exec rake spec の代わりに bundle exec rspec 出来た。

0
lafeber