RSpec和设计:无法登录管理员 [英] RSpec and devise : cannot sign_in admin
问题描述
我尝试在设计教程之后的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 :
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屋!