Rails 3.1 Gemmable Plugins with RSpec

I’ve had a bitch of a time lately getting the new Rails 3.1 plugin architecture to play nicely with RSpec. I actually gave up and coded an entire plugin with Test::Unit before finally figuring out a decent way to do it.

Currently, the new Rails 3.1 mountable plugin architecture will not work with RSpec. There’s a gem called Enginex that will work with RSpec, but it won’t work with Rails 3.1. So it looks like you either get RSpec or Rails 3.1. What’s the deal?

Playing around, I figured out that combining the two will work:

$ gem install enginex
$ enginex my_plugin --test-framework=rspec
$ rails -v
Rails 3.1.1
$ rails plugin new stock_plugin --full
$ rm -rf my_plugin/spec/dummy
$ cp -r stock_plugin/test/dummy my_plugin/spec

After that, it’s just a matter of modifying my_plugin/spec/dummy/config/application.rb to require your gem, and clean up the spec_helper.rb file. I also changed the Gem files so that my_plugin.gemspec held the dependencies, and Gemfile just called that. This is the way the stock Rails 3.1 generator works. Enginex uses Gemfile as the canonical source, and I like the new way better.

Using this I’ve currently got a Rails 3.1 gemmable plugin with RSpec, Cucumber, Capybara, Spork, Guard, and Simplecov working just fine.