加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 03:56:13 所属栏目:百科 来源:网络整理
导读:我将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_an
我将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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读