RSpec 和设计:无法登录管理员 [英] RSpec and devise : cannot sign_in admin

查看:21
本文介绍了RSpec 和设计:无法登录管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的标准 Rails 单元测试迁移到 RSpec,但我在设计方面遇到了问题.所有包含设备身份验证的控制器都因 RSpec 而失败.

I am migrating my standart Rails unit tests to RSpec and i have problems with devise. All controller containing devise authentication are failing with RSpec.

我按照设计教程尝试在 RSpec 中登录管理员,但没有成功:

I try to sign_in an admin in RSpec following the devise tutorial, without success :

https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-(and-rspec)

这是我尝试过的:

/spec/controllers/ipad_tech_infos_controller_spec.rb

/spec/controllers/ipad_tech_infos_controller_spec.rb

before :each do
    @request.env["devise.mapping"] = Devise.mappings[:admin]
    @admin = FactoryGirl.create :admin
    sign_in @admin
end

/spec/support/devise.rb

/spec/support/devise.rb

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

/spec/factories/admin.rb

/spec/factories/admin.rb

FactoryGirl.define do
  factory :admin do
    email "abc@abc.com"
    password "foobar"
    password_confirmation {|u| u.password}
  end
end

我的模型无法确认,我所有的控制器规格都失败了.

My model is not confirmable, all my controller spec are failing.

如果我删除 before_filter :authenticate_admin!然后我所有的测试都通过了.

If i remove before_filter :authenticate_admin! then all my tests pass.

有人可以帮忙吗?

推荐答案

可能的罪魁祸首:确保您没有在控制器规范中明确设置会话.

Likely culprit: Make sure you're not setting the session explicitly in your controller specs.

例如,如果您使用默认的 Rspec 脚手架生成器,则生成的控制器规范会传递会话参数.

For instance, if you're using the default Rspec scaffold generator, the generated controller specs pass along session parameters.

get :index, {}, valid_session

这些会覆盖 Devise 的助手为使用 Warden 登录而设置的会话变量.最简单的解决方案是删除它们:

These are overwriting the session variables that Devise's helpers set to sign in with Warden. The simplest solution is to remove them:

get :index, {}

或者,您可以在其中手动设置 Warden 会话信息,而不是使用 Devise 的助手.

Alternatively, you could set the Warden session information in them manually, instead of using Devise's helpers.

这篇关于RSpec 和设计:无法登录管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆