2017-10-24 6 views
0

私はRSpecのを使ってテストしたいカスタムAutorizationAdapterを持っている:私はAuthorizationAdapterが移動するための方法と思われたカスタムを使用して、工夫を使用していますので、activeadmin AuthorizationAdapterをテストするにはどうすればよいですか?

class AdminAuthorization < ActiveAdmin::AuthorizationAdapter 
    def authorized?(_action, _subject = nil) 
    user.admin? 
    end 
end 

当初、私は、カスタムメソッドを使用しますが。

どうすればテストできますか?私はそれがステータスコード&リダイレクトするためのコントローラとテストのいずれかの要求仕様を作成することですテストする一つの方法を取り払わ、そのようなこと:

require 'rails_helper' 

RSpec.describe 'AdminUsers', type: :request do 
    describe 'GET /admin_users' do 

    context 'admin' do 
     let(:admin_user) { create(:admin_user) } 
     before { sign_in super_user } 
     get admin_users_path 
     expect(response).to have_http_status(200) 
    end 

    context 'non admin' do 
     let(:user) { create(:user) } 
     before { sign_in user } 
     it 'redirects to the login page' do 
     get admin_users_path 
     expect(response).to have_http_status(302) 
     expect(response).to redirected_to '/admin/login' 
     end 
    end 

    context 'non logged in user' do 
     it 'redirects to the login page' do 
     get admin_users_path 
     expect(response).to have_http_status(302) 
     expect(response).to redirected_to '/admin/login' 
     end 
    end 
    end 
end 

私は、これが移動するための方法であるか分かりません。

答えて

0

これらは私にとって妥当なものです。また、ActiveAdminテストスイートに含まれているユニットとフィーチャーの仕様を見ることもできます。ただし、AuthorizationAdapter自体はPOROなので、単体テストを行うことができるはずです。上の例では、これはかなり簡単なテストです。

関連する問題