19

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:

  1. Add a rails test app to the root of the gem (myengine) by running: rails new /myengine/rails_app

  2. Add Cucumber to /myengine/rails_app/features as you would in a normal Rails app

  3. Require the Rails Engine Gem (using :path=>"/myengine") in /myengine/rails_app/Gemfile

  4. Add spec to the root directory of the gem: /myengine/spec

  5. Include the fixtures in /myengine/spec/fixtures and 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.

Ryan Bigg
  • 102,687
  • 22
  • 224
  • 252
johnmcaliley
  • 10,779
  • 1
  • 39
  • 47

3 Answers3

6

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:

rvm gemset create edge
rvm use @edge

Then install edge rails:

git clone git://github.com/rails/rails.git
cd rails
rake install

From there, you can follow Piotr Sarnacki's mountable app tutorial, replacing calls such as:

bundle exec ./bin/rails plugin new ../blog --edge --mountable

With simply:

rails plugin new blog --mountable --full

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 test called dummy which contains a small Rails application. You can see how this is loaded in test/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:install into 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.

Ryan Bigg
  • 102,687
  • 22
  • 224
  • 252
  • Ryan, thanks for the info. That is what I was looking for. Looks like some good improvements are coming in 3.1. Having trouble installing rails edge, though. "ERROR: Could not find a valid gem 'pkg/rails-3.1.0.beta.gem' (>= 0) in any repository" All the other gems (AR,AS,etc..) build and install fine, but it doesn't look like the Rakefile is building the rails gem. Maybe something with my environment... I just started troubleshooting it, but if you have a minute, let me know if you are able to install edge from the rake task without error. Thanks man! – johnmcaliley Jan 22 '11 at 15:15
  • I messed around with it for a bit with no luck and ended up building the gem manually in the pkg dir. Then rake install worked, since the gem was there. Not sure why it did not work out of the box.. – johnmcaliley Jan 22 '11 at 23:05
  • Also, there seems to be a bug with the MIT-LICENSE in the templates dir. erb cant find Date.today... maybe the method was removed dynamically? I just hard-coded the date into the template as a temporary workaround. Other than that the template looks solid. thanks again – johnmcaliley Jan 23 '11 at 20:40
  • Same error here as well. ERROR: Could not find a valid gem 'pkg/rails-3.1.0.beta.gem' (>= 0) in any repository – mculp Apr 08 '11 at 04:47
2

I'll explain how I did it using as example the following gem: https://github.com/skozlov/netzke-core

The testing application. It is in netzke-core/test/rails_app. This app can be run independently, so I can also use it for manual testing or for playing around with new features if I like.

In order for the testing app to load the gem itself, I have the following in application.rb:

$:.unshift File.expand_path('../../../../lib', __FILE__)
require 'netzke-core'

Cucumber features. They are in netzke-core/features. In env.rb I have:

require File.expand_path(File.dirname(__FILE__) + '/../../test/rails_app/config/environment')

... which will load the testing application before executing the features.

Specs. These are in netzke-core/spec. In spec_helper.rb I have the following:

require File.expand_path("../../test/rails_app/config/environment", __FILE__)

... which will load the testing application before running the specs.

Running tests. This setup lets me run the tests from the root of the gem:

cucumber features

and

rspec spec

Factory Girl. Not for this particular gem, but I'm normally using factory_girl instead of fixtures (see, for example, a similar setup in https://github.com/skozlov/netzke-basepack).

mxgrn
  • 1,683
  • 16
  • 20
0

A bit late to the party, but here is my strategy:

  1. Generating the rails plugin in 3.2:

    rails plugin new blog --mountable --full
    

    This creates test/dummy, containing the dummy rails app

  2. Add the specs to spec

  3. Move the dummy folder to spec (and optionally get rid of the other testfiles)

  4. Adapt specs/spec_helper.rb so it includes

    require File.expand_path("../.../config/environment", __FILE__)
    

    instead of

    require File.expand_path("../dummy/config/environment", __FILE__)
    
  5. Execute rails g cucumber:install. It will generate features folder a.o.

  6. Add

    ENV["RAILS_ROOT"] ||= File.expand_path(File.dirname(__FILE__) + '/../../spec/dummy')
    

    before

    require 'cucumber/rails'
    

    in features/support/env.rb

Now you have features and spec in the root of you project, while the dummy rails app is neatly tucked away under spec/dummy

bert bruynooghe
  • 2,761
  • 1
  • 18
  • 18