I apologize if this question is slightly subjective… I am trying to figure out the best way to test Rails 3 Engines with Cucumber & Rspec. In order to test the engine a rails 3 app is necessary. Here is what I am currently doing:
-
Add a rails test app to the root of the gem (myengine) by running:
rails new /myengine/rails_app -
Add Cucumber to
/myengine/rails_app/featuresas you would in a normal Rails app -
Require the Rails Engine Gem (using
:path=>"/myengine") in/myengine/rails_app/Gemfile -
Add spec to the root directory of the gem:
/myengine/spec -
Include the fixtures in
/myengine/spec/fixturesand I add the following to my cuc env.rb:
env.rb:
Fixtures.reset_cache
fixtures_folder = File.join(Rails.root, 'spec', 'fixtures')
fixtures = Dir[File.join(fixtures_folder, '*.yml')].map {|f| File.basename(f, '.yml') }
Fixtures.create_fixtures(fixtures_folder, fixtures)
Do you see any problems with setting it up like this? The tests run fine, but I am a bit hesitant to put the features inside the test rails app. I originally tried putting the features in the root of the gem and I created the test rails app inside features/support, but for some reason my engine would not initialize when I ran the tests, even though I could see the app loading everything else when cuc ran.
If anyone is working with Rails Engines and is using cuc and rspec for testing, I would be interested to hear your setup.
**UPDATE
I changed my setup a bit since I wrote this question. I decided to get rid of the spec directory under the root of the engine. Now I just create a rails app named “test_app” and setup cuc and rspec inside that app like I would normally do in a rails app. Then I include the gem like I did in step #3 above. Since the engine is a sub-app, I guess its just best to test it like it was a normal rails app. I am still interested in hearing if anyone has a different setup.
Rails 3.1 (will) generate a pretty good scaffold for engines. I’d recommend using RVM to create a new gemset called edge and switch to it:
Then install edge rails:
From there, you can follow Piotr Sarnacki’s mountable app tutorial, replacing calls such as:
With simply:
The mountable option makes the application mountable, whilst the full option makes it an engine with tests already built-in. To test the engine, this generator generates a folder in
testcalleddummywhich contains a small Rails application. You can see how this is loaded intest/test_helper.rb.Then it’s up to you to massage the data to do what it needs to in order to work. I would recommend copying over the cucumber files from a standard
rails g cucumber:installinto the project and then messing about with it until it works. I’ve done this once before so I know it’s possible, but I cannot find the code right now.Let me know how you go.