Skip to content

Testing ActiveAdmin within another gem using RSpec and Combustion

Eike Send edited this page Feb 16, 2018 · 2 revisions

This page used to be called "Testing your ActiveAdmin controllers with RSpec"

The problem

During the creation of the gem ActiveTrail integrating ActiveAdmin with Trailblazer the controller was customized, something like this:

ActiveAdmin.register Book do
  # everything happens here :D
  controller do
    include ActiveTrail::Controller
  end
end

And how to test the controller and make some requests? The routes created dynamically by AA wasn't available when running the test suite, so I inspected AA test suite to see how its done, and here is how the problem was solved:

The solution

This solution can be checked on this commit

Supposing that we want to make a simple test like this:

RSpec.describe Admin::BooksController, :type => :controller do 
  describe "GET index" do
    it "assigns books" do
      book = Book.create
      get :index
      expect(assigns(:books)).to eq([book])
    end
  end
end

Combustion

As we are testing a gem that uses Rails, we used Pat's Combustion gem to create a dummy Rails application inside our gem so we can make controllers and requests tests, check the gem page and our commit how to set it up.

  1. Setup Combustion
  2. Add a spec/internal/config/initializers/activeadmin.rb file
  3. Create resource and comments table on spec/internal/db/schema.rb
  4. Edit rspec spec/spec_helper.rb and spec/support/integration_example_group.rb

Spec Helper

I copied spec helper and integration example group from AA repo, so we have load_default! and reload_routes! methods available.

Then in our rspec config we can load AA stuff before any test and all routes and resources will be available.

In activetrail case it was added on rspec config because we just have AA tests, but if you have more, just run this before(:each) on the specific tests.

Check the full spec_helper.rb and integration_example_group.rb for a complete example.

ps: After you got controllers tests working, requests will be ok too.

Clone this wiki locally