visit our site

Testing gem integration with multiple ruby frameworks

In Random

by Andriy Yanko by January 7, 2012


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.rb

Write acceptance spec for example in spec/middleware_spec.rb :
Follow DRY principle and use shared_examples!

Run tests

Before implementation run specs

Now 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.


* Railsware is a premium software development consulting company, focused on delivering great web and mobile applications. Learn more about us.
  • Macario Ortega

    Hi, this is great stuff!!, one of the best testing tips I’ve read.
    Off course I would like to see more framework agnostic gems. Why do people write Rails gems instead of Ruby gems?

    I’ve just had to add this line:

    Rails.logger =

    Because I was getting this error: undefined method info for nil class

    With this I can start testing some agnostic markup builder dsl and some view helpers, only thing I am missing is that errors raised in the rails app are rescued, I can see in the error printed because of the logger.

  • Macario Ortega

    Also I haven’t been able to render haml templates.

Signup for our weekly newsletter

Want to get more of Railsware blog?

RSS Feed

We're always ready to help!

Contact us