Tag Archives: rack

Multiple Sinatra Apps and Cucumber

How do I test a stack of modular (multiple) Sinatra applications with Cucumber? Honestly, I was a bit baffled about how I would get my env.rb to load my config.ru the way it should be loaded. Specifically, I wanted Cucumber to run features with my Rack::Map and other middleware just the same as when my server actually starts. There’s nothing short of several thousand blog posts about Cucumber usage and setup, but I couldn’t find anything about how to do this. Here is an example of Cucumber with Capybara env.rb loading the config.ru with Rack::Builder.

ENV['RACK_ENV'] = 'test'
require File.expand_path(File.dirname(__FILE__) + "/../../init")
require 'capybara'
require 'capybara/cucumber'
require 'spec'
class SinatraWorld
require "selenium-webdriver"
Capybara.default_driver = :selenium
# use the rackup file to load the apps w/their respective URL mappings, sweet!
Capybara.app = eval "Rack::Builder.new {( " + File.read(File.dirname(__FILE__) + '/../../config.ru') + "\n )}"
include Capybara
include Spec::Expectations
include Spec::Matchers
World do

Since the Sinatra applications are defined in the config.ru with Rack::Map, I’m not limited to just a singular Sinatra app (as so many examples demonstrate).

Here is an example of the config.ru:

require File.expand_path(File.dirname(__FILE__) + "/init")
use Rack::Static, :urls => ['/javascripts', '/stylesheets', '/images', '/html'], :root => File.join(File.dirname(__FILE__), 'public')
use Rack::Lint
map "/app1" do
  run SinatraAppOne
map "/app2" do
  run SinatraAppTwo
map "/app3" do
  run SinatraAppThree

Silicon Valley Ruby Meetup – Sinatra

More to come…


Recorded audio from the presentation

Unbeknownst to me, Sinatra was once of the presenations at #RubyKaigi 2010 in Japan. What’s really amazing to me is that on the other side of the world two speakers were basically spreading the good word in a very similar way. Of the many similarities between our presentations, one was that we were reinforcing the notion of Rack being the fundamental and foundational awesomeness of Sinatra. Here is the link to Jiang Wu’s presentation http://rubykaigi.tdiary.net/20100829.html#p08.

Check out how Sinatra’s syntactic sugar is spreading in other languages. Have a look at express, it is Sinatra like framework based on node.js.