11

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

管理者のセッションをチェックして、彼が実際にサインインしているかどうかを確認する別の方法はありますか?

4

4 に答える 4

10
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にあります。

于 2013-04-03T11:27:52.820 に答える
7

本当に新しい答えではありませんが、私の担当者はコメントするのに十分な高さではありません...:

  • すでにオーバーライドしている場合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
    
于 2013-12-05T19:39:17.363 に答える