BDD Rails engines with RSpec
I am big fan of Rails engines. We use them a lot in our company to reduce code duplication. Rails 3.1 introduced a number of great improvements and finally made it possible to write tests without going through hassle of manually creating dummy application. But what if you want to use RSpec instead of TestUnit? It doesn't quite work out of the box and requires a few small changes.
First make sure you generate generate the engine with -T flag to avoid generating TestUnit code. It is also a good idea to change path of the dummy app to spec/dummy just for consistency. E.g.:
rails plugin new my engine -T --dummy-path=spec/dummy -T
As always you need to add 'rspec-rails' to your Gemfile and run bundle command and do rspec install.
bundle install rails g rspec:install
By default spec_helper.rb will try to load environment of your rails app but in case of the engine we need to load dummy app, so change the first require statement to
require File.expand_path("../../spec/dummy/config/environment", __FILE__)
Finally you might want enable rspec generators, though the code generated will no be fully compatible with engines at least you'll have correct files in place. Though I personally prefer to create files manually. Just the following piece of code to you lib/myengine/engine.rb:
config.generators do |g| g.test_framework :rspec g.integration_tool :rspec end











