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

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

问题描述

我正在将我的标准Rails单元测试迁移到RSpec,我有设计问题。所有使用RSPec的控制器都将失败。



我尝试在设计教程之后的RSpec中签署一个管理员,没有成功:



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



这是我试过的:



/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

  RSpec.configure do | config | 
config.include Devise :: TestHelpers,:type => :控制器
end

/spec/factories/admin.rb

  FactoryGirl.define do 
工厂:admin do
电子邮件abc@abc.com
密码foobar
password_confirmation {| u | u.password}
end
end

我的模型不可确认,所有我的控制器规格是失败的。



如果我删除before_filter:authenticate_admin!那么所有的测试都会通过。



有人可以帮助吗?

解决方案

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



例如,如果您使用默认Rspec脚手架生成器,生成的控制器规范通过会话参数。

  get:index,{},valid_session 

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

  get:index,{} 
/ pre>

或者,您可以手动设置监狱长会话信息,而不是使用Devise的助手。


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.

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)

Here is what i tried :

/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

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

/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.

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

Can anybody help ?

解决方案

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

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

get :index, {}, valid_session

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, {}

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

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

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