Testing gem integration with multiple ruby frameworks


When you develop some cool gem that should work in different ruby frameworks you definitely should write acceptance tests.Nowadays it’s pretty easy to do with RSpec+Capybara.

Your goal

Assume you develop MyRackMiddleware gem that should work in Sinatra and Rails application.

Your actions

Create gem layout

To create layout use well-known bundle gem command: It’s generate something like:Add to Gemfile :Now start use BDD :) Before start implementation we need two sample applications. Let’s put they into apps directory.

Creating Sinatra test application

Thus Sinatra is simple framework it’s trivial to create test application:

Creating Rails3 test application

When you think that with rails it will be hard then your are wrong :) Thanks to rails3 design its also rather simple! Let’s see:So you have whole rails application in one file. Nice! :)

Write integration tests

Now is most interesting part of this article. Capybara gem itself has rspec helpers that allows you to write rspec examples in way of acceptance testing (aka feature and scenario).Actually internally feature is just alias to ExampleGroup (describe) and scenario is alias to Example (it). But this syntax follows you to think different.Put in spec/spec_helper.rbWrite acceptance spec for example in spec/middleware_spec.rb : Follow DRY principle and use shared_examples!

Run tests

Before implementation run specsNow you may add scenarios that cover your MyRackMiddleware influence on applications and start your middleware implementation!


We hope this article will help you to write better tests for your gems.