ruby-on-rails – 用于nil的未定义方法`env’:NilClass

我将rspec从版本2升级到3.之后我遇到了这个问题:

Failures:

  1) AlbumsController GET #edit 
     Failure/Error: sign_in_and_switch_schema @user
     NoMethodError:
       undefined method `env' for nil:NilClass
     # ./spec/support/auth_helpers.rb:10:in `sign_in_and_switch_schema'
     # ./spec/controllers/albums_controller_spec.rb:12:in `block (2 levels) in <top (required)>'

spec_helper.rb包含:

RSpec.configure do |config|
    # most omitted
    config.include Warden::Test::Helpers
    config.include Devise::TestHelpers,type: :controller
 end

albums_controller_spec.rb:

describe AlbumsController do

  let(:album) { create(:album) }

  before(:all) do
    @user = create :user
  end

  before(:each) do
    sign_in_and_switch_schema @user
  end

  after(:all) do
    destroy_users_schema @user
    destroy_user @user
  end

 # describe's part omitted
end

auth_helpers.rb发生错误的部分:

def sign_in_and_switch_schema(user)
 # binding.pry
 @request.env["devise.mapping"] = Devise.mappings[:user] # <- error line
 sign_in :user,user

 Apartment::Tenant.switch(user.username) 
end

我正在寻找另一个同样的Q& A,但没有找到任何帮助.如果我应该包含更多内容,请告诉我.提前致谢.

解决方法

解决方法是添加到spec_helper.rb:

RSpec.configure do |config|
    config.infer_spec_type_from_file_location!
end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...