“未定义的方法”env“for nil:NilClass”在使用Rspec测试Devise时出现'setup_controller_for_warden'错误 [英] "undefined method `env' for nil:NilClass" in 'setup_controller_for_warden' error when testing Devise using Rspec
问题描述
我试图通过使用factorygirl创建一个用户来创建一个登出流的规范,然后使用Devise的 sign_in
方法来验证用户,然后使用capybara点击注销链接。
I'm trying to create a spec for a sign out flow by using factorygirl to create a user and then use Devise's sign_in
method to authenticate the user, then use capybara to click the "Sign Out" link.
当我运行规范时,我正在(我似乎觉得是什么)出现了一个奇怪的错误:
I'm getting (what seems to me to be) a strange error when I run the spec:
Failures:
1) Sign out flow successfully redirects to the welcome index (root)
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `env' for nil:NilClass
# /home/vagrant/.rvm/gems/ruby-2.0.0-p576/gems/devise-3.4.1/lib/devise/test_helpers.rb:24:in `setup_controller_for_warden'
Finished in 0.00226 seconds (files took 3.32 seconds to load)
1 example, 1 failure
这是规范:
require 'rails_helper'
describe "Sign out flow" do
include Devise::TestHelpers
describe "successfully" do
it "redirects to the welcome index (root)" do
user = create(:user)
sign_in user
within '.user-info' do
click_link 'Sign Out'
end
expect(current_path).to eq root_path
end
end
end
而我的 user.rb
factory:
And my user.rb
factory:
FactoryGirl.define do
factory :user do
name "Fake User"
sequence(:email, 100) { |n| "person#{n}@example.com" }
password "helloworld"
password_confirmation "helloworld"
confirmed_at Time.now
end
end
错误似乎只是从行包含Devise :: TestHelpers
,因为我已经尝试评论整个规范的内容,并仍然得到相同的错误。
The error seems to be triggered simply from the line include Devise::TestHelpers
, as I've tried commenting out the entire content of the spec and still get the same error.
我以为Devise测试工作人员将从框;我错过了一些配置吗?谢谢。
I thought the Devise test helpers would work out of the box; did I miss some configuration? Thanks.
推荐答案
显然,有 Devise :: TestHelpers
和集成测试,所以也许是这里的问题。
Apparently there are issues with Devise::TestHelpers
and integration testing, so perhaps that's the problem here.
https://github.com / plataformatec / devise (在README,问题等中提及;还可以看到相关的SO问题):
https://github.com/plataformatec/devise (mentioned in README, Issues, etc.; also see related SO questions):
这些帮助者是不会为由Capybara或Webrat驱动的集成测试工作。它们仅用于功能测试。而是填写表单或明确地设置会话中的用户;
These helpers are not going to work for integration tests driven by Capybara or Webrat. They are meant to be used with functional tests only. Instead, fill in the form or explicitly set the user in session;
这篇关于“未定义的方法”env“for nil:NilClass”在使用Rspec测试Devise时出现'setup_controller_for_warden'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!