“nil:NilClass 的未定义方法‘env’";使用 Rspec 测试设计时出现“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
方法对用户进行身份验证,然后使用水豚单击注销"来为注销流程创建规范" 链接.
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
工厂:
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
该错误似乎只是从 include 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;
这篇关于“nil:NilClass 的未定义方法‘env’";使用 Rspec 测试设计时出现“setup_controller_for_warden"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!