Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未定义的方法'env';零级:零级_Ruby On Rails_Rspec_Devise_Rspec3 - Fatal编程技术网

Ruby on rails 未定义的方法'env';零级:零级

Ruby on rails 未定义的方法'env';零级:零级,ruby-on-rails,rspec,devise,rspec3,Ruby On Rails,Rspec,Devise,Rspec3,我将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_

我将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)>'
相册\u控制器\u规范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
def登录和切换模式(用户)
#捆绑,撬动
@request.env[“designe.mapping”]=designe.mappings[:user]#根据,您应该只使用

@request.env["devise.mapping"] = Devise.mappings[:user]
测试继承Desive控制器的控制器时。我猜AlbumsController不是从Desive控制器继承的。我认为这些测试不需要这条线

尝试删除此行或创建另一个仅执行登录和切换的帮助器方法:

def simple_sign_in_and_switch(user)
  sign_in :user, user
  Apartment::Tenant.switch(user.username)
end
然后在测试用例中调用该方法:

before(:each) do
  simple_sign_in_and_switch_schema @user
end

如果这样做有效,试着完全移除故障线路,运行完整的测试套件,看看是否需要其他地方。如果是,请用另一种方法提取该行,并仅在需要时使用。

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

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

错误发生在第10行的sign_in_和switch_schema方法的spec/support/auth_helpers.rb中。您能显示该文件的内容吗?抱歉,我错过了一个-Qupdated@mlainez:我在auth_helpers.rb中添加了
binding.pry
。它清楚地表明,
@request
nil
。我不知道我还能用它做什么,但如果你告诉我,我可以检查一下。现在它显示了
NoMethodError:undefined方法“sign\u in”
。我认为
config.include designe::TestHelpers、type::controller
不再适用于rspec3。我试图删除
type::controller
部分,但它显示了大堆栈跟踪,甚至没有进入
pry
部分。查看此线程,它可能会帮助您了解发生了什么=>我找到了解决方案<代码>配置。从文件位置推断规格类型。无论如何,谢谢你的时间和努力。你给我的链接显示了另一种指定规范类型的方法。这也是正确的。
RSpec.configure do |config|
    config.infer_spec_type_from_file_location!
end