All posts

2 min read Nathan Broadbent

Dynamically Generate Controller Specs for ActiveAdmin

Admin resources feed a generated matrix of passing index and show tests.

I use ActiveAdmin (and the arctic_admin theme) to build DocSpring’s internal admin dashboard:

ActiveAdmin has a few quirks and bugs, but I enjoy working with it overall. Some other popular admin frameworks for Rails are Administrate and RailsAdmin.

Whichever framework you’re using, it’s a good idea to write some tests. It’s always frustrating when something crashes, especially when you’re trying to help a customer.

Just like ActiveAdmin automatically generates an admin UI for your models, we can automatically generate some RSpec tests for ActiveAdmin. I use the following code to iterate over ActiveAdmin resources, create models using any FactoryBot factories, and ensure that I can visit the index and show routes without crashing:

# frozen_string_literal: true

ActiveAdmin.application.namespaces[:admin].resources.each do |resource|
  resource_name = resource.resource_name
  resource_title = resource_name.human.titleize
  has_factory = FactoryBot.factories.any? do |factory|
    factory.name.to_s == resource_name.singular
  end

  RSpec.describe resource.controller, type: :controller do
    let!(:user) { create(:user, :admin) }
    let(:page) { Capybara::Node::Simple.new(response.body) }
    let!(:model) do
      create(resource_name.singular) if has_factory
    end

    render_views

    before(:each) { sign_in user }

    it 'renders the index page' do
      get :index
      expect(page).to have_content(resource_title)
      if model
        show_path = send("admin_#{resource_name.singular}_path", model)
        expect(page).to have_link(model.id, href: show_path)
      end
    end

    if has_factory
      it 'renders the show page' do
        get :show, params: { id: model.to_param }
        expect(page).to have_content("#{resource_title} Details")
        expect(page).to have_content(model.name) if model.respond_to?(:name)
      end
    end
  end
end

Save this as spec/controllers/admin/autogenerated_controller_spec.rb, so RSpec discovers it with its default *_spec.rb pattern. This example assumes an :admin namespace, Devise’s sign_in test helper, a :user, :admin factory, and resources with both index and show actions. Adapt the factories, authentication, page labels, and enabled actions to your application. It is a rendering smoke test, not a replacement for authorization or behavior tests.

See also: Testing your ActiveAdmin controllers with RSpec

While I was working on this, I discovered that some of my FactoryBot factories were crashing, because I had never used them without arguments. I set up a factory_bot:lint Rake task to make sure that all of my factories can create valid models. I’ve started running this task during my CI build.

More posts

  1. A cut ruby gemstone fitted exactly inside a navy blueprint outline, connected to four code cards with teal check marks, above a tag reading typed: strict.

    14 min read

    Going all-in on Sorbet

    Sorbet sat in our Rails app for 18 months with a tenth of the code typed. Then AI agents typed the rest in four days, and we kept going.

  2. An SDK card and an API card, each connected by a line to a filled PDF form

    4 min read

    PDF form filler SDK vs API

    When a PDF library is enough to fill your forms, and when a hosted API saves you the work.

  3. A JSON data card connected by a line to a PDF form with filled fields, a ticked checkbox and a signature

    5 min read

    Best PDF form filling APIs (2026)

    DocSpring, Anvil, PDF.co, Adobe PDF Services, and Nutrient compared for filling fillable PDFs and flat PDFs.

Try DocSpring with your own documents

Upload a PDF or a Word document, or create an HTML template, then send a test request. No credit card needed.

Blog posts and product news by email

New articles and product changes, once a month.