簡體   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