Skip to content

Testing your ActiveAdmin controllers with RSpec

Celso Fernandes edited this page Mar 29, 2015 · 9 revisions

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 dinamically 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 we wanna 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 setup it.

  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