Testing Rails Engine

Hey All,

Am building a gem that adds a user interface to another gem. I’m struggling with the best way to test this with hitting the database to make sure that roles are being set after specific calls.

Any recommendations on where to start researching this? I’ve already checked the normal places like edge rails.

Hi @Austin,

As a general overview, you’re going to want to write integration specs for your gem. The integration specs will actually include a rails app in it which includes your gem and the other gem in the Gemfile, and will test them together.

I hope that points you in the right direction, let me know if I can provide any information or how it goes.


Hey thanks @cpytel

I meant more from how to populate the dummy app? Do you setup a seed file, include a sqlite3 database or do something like fixtures/factorygirl?

I think factory girl would be super helpful for engine testing, is that what you use?

Yes, I recommend using FactoryGirl for test data, even with engines.

Even if you use FactoryGirl, you do still need the dummy/test Rails app to have a database, otherwise the app won’t run. But you don’t need to include an sqlite database, in fact we generally recommend using Postgres for everything. Either way, you can rake db:create either manually or as part of your automated test run, from within the test application and the database will be created if it doesn’t already exist.

From there, you can use your normal testing strategies within the app. FactoryGirl, relying as much as possible on build_stubbed, and also Rspec test doubles.

I hope that helps,

Hey @cpytel

I’m looking into integration testing an Engine subscription gem. Are you saying that FactoryGirl does not need a Rails model but simply any fixture can be created and used? Then as you suggest use test doubles and stubbing?

Also where would the rake db:create be located for testing?

-Chris / Chewy

Hi @ChrisCPO,

While FactoryGirl doesn’t need a Rails model, and can be used against normal Ruby classes, that is not what I was referring to here. What I am saying here is that FactoryGirl can be used within the testing of the Rails Engine in the same way that it can be used to test a normal Rails app.

Also where would the rake db:create be located for testing?

You can either run it manually or automate it as part of your test run.

For a concrete example of how this is done in practice, you can take a look at the specs for Clearance. In particular, here is the spec that tests the installation of clearance by creating a new Rails app and installing Clearance in it in an automated fashion. And there is also the dummy rails application that is included in the spec helper.