-
Notifications
You must be signed in to change notification settings - Fork 0
Testing your ActiveAdmin controllers with RSpec
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:
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
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.
- Setup Combustion
- Add a
spec/internal/config/initializers/activeadmin.rb
file - Create resource and comments table on
spec/internal/db/schema.rb
- Edit rspec
spec/spec_helper.rb
andspec/support/integration_example_group.rb
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.