【发布时间】:2019-03-24 13:21:37
【问题描述】:
首先我阅读了有类似问题的用户的其他帖子,但无法找出我的错误所在。我想用 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
在顶部的 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 进行测试的文档后,我更改了 dashboard_view_spec.rb 中的代码,并习惯于以 user 身份sign_in 并获得了同样的错误信息。
devise.rb 的第 258 行
config.omniauth :facebook, Rails.application.secrets.facebook[:key], Rails.application.secrets.facebook[:secret], scope: 'email', info_fields: 'email, name'
在宝石文件中
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
【问题讨论】:
-
你能添加完整的堆栈跟踪和执行的命令吗?
-
我添加了命令。错误的输出上没有堆栈跟踪。我截图了终端的错误信息。
-
我改了。希望现在更好。感谢您的提示!
-
您还没有发布
config/initializers/devise.rb:258中的内容。错误消息非常简单:第 258 行的某些内容试图在一个对象上执行[],该对象预期是Array,但实际上是nil。找到那个对象,找出它为什么是nil而不是Array,然后修复它。很可能它引用了在您的development环境中定义的内容,而不是您的test环境中定义的内容,这就是您在test环境中出现此错误的原因。对测试的其余部分进行更改并不重要 - 在您的规范运行之前,它在 第 1 行 上失败了。 -
我在开始的帖子中添加了这一行。
标签: ruby-on-rails ruby ruby-on-rails-4 rspec