消息:失败/错误:需要File.expand_path('../../ config / environment',__FILE__)

Jam*_*ich 5 ruby rspec ruby-on-rails ruby-on-rails-4

首先,我阅读了其他有类似问题的用户的帖子,但我的错误所在无法解决。我想对以下文件使用RSpec进行测试:

dashboard_view_spec.rb:

require 'rails_helper'

RSpec.feature "Dashboard", type: :feature do 
before(:each) do
@current_user = User.create!(email: "xyz@xyz.com", 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
Run Code Online (Sandbox Code Playgroud)

在顶部的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'
Run Code Online (Sandbox Code Playgroud)

错误信息:

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
Run Code Online (Sandbox Code Playgroud)

然后我在终端上使用的命令

 bundle exec rspec spec/views/dashboard_view_spec.rb
Run Code Online (Sandbox Code Playgroud)

看着与设计测试的文件后,我改变了代码 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'
Run Code Online (Sandbox Code Playgroud)

在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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

ano*_*rmh 6

问题是您缺少设计初始值设定项希望找到的变量,但仅在test环境中缺少它。(不是development环境)

您提供的堆栈跟踪和第 258 行config/initializers/devise.rb都是需要的。首先,让我们看一下堆栈跟踪:

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)>'
Run Code Online (Sandbox Code Playgroud)

从下往上,我们可以看到规范的第一行是导致问题的原因。如果您跟踪堆栈,您会看到它最终进入config/initializers/devise.rb:258并抱怨进行[]方法调用的某些东西没有得到预期的对象类型作为回报,而是得到nil了回报。

查看第 258 行,您会发现:

config.omniauth :facebook, Rails.application.secrets.facebook[:key],  Rails.application.secrets.facebook[:secret], scope: 'email', info_fields: 'email, name'
Run Code Online (Sandbox Code Playgroud)

所以你可以看到有两次[]被调用:

Rails.application.secrets.facebook[:key]
Rails.application.secrets.facebook[:secret]
Run Code Online (Sandbox Code Playgroud)

预期调用Rails.application.secrets.facebook将返回一个Enumerable对象(如数组或散列),但它返回的是nil,并且当它尝试运行nil[:key]nil[:secret]引发异常时,因为NilClass不是 anEnumerable并且没有[]方法。您可以在以下位置自己测试rails console

nil[:secret]
=> NoMethodError: undefined method `[]' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)

解决方案是确保调用Rails.application.secrets.facebook返回预期的对象类型。简短的回答是进行编辑config/secrets.yml以确保test存在环境所需的值。

我已经很长时间没有使用设计了,但我假设您可以安全地使用与development环境中使用的相同的值作为test环境。您可以在secrets()别处阅读更多相关信息,但基本模板config/secrets.yml如下:

environment:
  key: value
Run Code Online (Sandbox Code Playgroud)

例如:

test:
  my_secret_key: my_secret_value
Run Code Online (Sandbox Code Playgroud)

将丢失的facebook秘密复制并粘贴到test环境中应该相当简单。进行更改后,您可以验证它是否有效:

$ RAILS_ENV=test rails console
Rails.application.secrets.facebook[:key]
=> <your key here>
Run Code Online (Sandbox Code Playgroud)

如果可行,然后运行您的规范,rspec它应该成功通过第 1 行。(假设没有其他错误或丢失的秘密)