【问题标题】:How to test activeadmin AuthorizationAdapter?如何测试activeadmin AuthorizationAdapter?
【发布时间】:2017-10-24 10:18:39
【问题描述】:

我有一个自定义 AutorizationAdapter,我想使用 RSpec 进行测试:

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

最初我使用自定义方法,但由于我使用的是 Devise,因此使用自定义 AuthorizationAdapter 似乎是可行的方法。

您将如何进行测试?我认为测试它的一种方法是为其中一个控制器创建请求规范并测试状态代码和重定向,如下所示:

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

我不确定这是要走的路。

【问题讨论】:

    标签: ruby-on-rails rspec devise activeadmin


    【解决方案1】:

    这些在我看来是合理的。您还可以查看 ActiveAdmin 测试套件中的单元和功能规范。但是,AuthorizationAdapter 本身是一个 PORO,因此您应该能够单独进行单元测试:在上面给出的示例中,这将是一个相当简单的测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 2013-10-16
      相关资源
      最近更新 更多