繁体   English   中英

检查用户是否已在设计中注销

[英]Check if a user is signed out in devise

我正在尝试检查管理员是否在Rspec测试中注销。 但通常的signed_in? 方法不能从rspec中看出,也不是RSpec Devise Helpers的一部分。

这样的事情就是我所拥有的

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

有没有办法检查管理员的会话,看看他是否真的登录?

我认为这真的是你需要的如何:使用Rails 3和4(以及RS​​pec)测试控制器

只需检查current_user 它应该是nil

加。 好的做法是使用这样的语法

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

发现于https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29

真的,这不是一个新的答案,但我的代表不够评论......:

  • 如果您已经覆盖的subject ,该控制器可作为controller在控制器的规格,所以:

     expect { ... }.to change { controller.current_user }.to nil 
  • 为了检查特定用户,比如说是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

你可以参考这个链接设计规范帮助链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM