[英]Check if a user is signed out in devise
I'm trying to check if an administrator is signed out in an Rspec test. 我正在尝试检查管理员是否在Rspec测试中注销。 However the usual signed_in? 但通常的signed_in? method can't be seen from rspec and isn't part of the RSpec Devise Helpers. 方法不能从rspec中看出,也不是RSpec Devise Helpers的一部分。
Something like this is what i have in place 这样的事情就是我所拥有的
before (:each) do
@admin = FactoryGirl.create(:administrator)
sign_in @admin
end
it "should allow the admin to sign out" do
sign_out @admin
#@admin.should be_nil
#@admin.signed_in?.should be_false
administrator_signed_in?.should be_false
end
Is there anothe way to check the session of the administrator and see if he's actually signed in or not? 有没有办法检查管理员的会话,看看他是否真的登录?
I think it's really what you need How To: Test controllers with Rails 3 and 4 (and RSpec) 我认为这真的是你需要的如何:使用Rails 3和4(以及RSpec)测试控制器
Just check current_user
. 只需检查current_user
。 It should be nil
它应该是nil
Add. 加。 Good practice is using syntax like this 好的做法是使用这样的语法
-> { sign_out @admin }.should change { current_user }.from(@admin).to(nil)
it "should have a current_user" do
subject.current_user.should_not be_nil
end
Found at https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29 发现于https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29
Not a new answer, really, but my rep isn't high enough to comment...: 真的,这不是一个新的答案,但我的代表不够评论......:
If you've already overridden subject
, the controller is available as controller
in controller specs, so: 如果您已经覆盖的subject
,该控制器可作为controller
在控制器的规格,所以:
expect { ... }.to change { controller.current_user }.to nil
To check for a specific user, say generated by FactoryGirl, we've had good success with: 为了检查特定用户,比如说是FactoryGirl生成的,我们取得了很好的成功:
let(:user) do FactoryGirl.create(:client) ; end ... it 'signs them in' do expect { whatever }.to change { controller.current_user }.to user end it 'signs them out' do expect { whatever }.to change { controller.current_user }.to nil end
it "signs user in and out" do user = User.create!(email: "user@example.org", password: "very-secret") sign_in user expect(controller.current_user).to eq(user) sign_out user expect(controller.current_user).to be_nil end
You can refer this link devise spec helper link 你可以参考这个链接设计规范帮助链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.