我先阅读了其他用户发布的与我的问题类似的帖子,但是没有找到解决方法。我想要在以下文件中使用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 中的代码并使用 sign_in 作为 user,但是得到了相同的错误信息。
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
and
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
环境中会遇到这个错误。对你的测试的其余更改无关紧要——它在 第一行 失败,即在运行你的规范之前。 - anothermh