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
end
 
World do
SinatraWorld.new
end

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
end
 
map "/app2" do
  run SinatraAppTwo
end
 
map "/app3" do
  run SinatraAppThree
end

2 thoughts on “Multiple Sinatra Apps and Cucumber”

  1. You can use this
    Capybara.app, _ = Rack::Builder.parse_file(File.expand_path(‘../../config.ru’, __FILE__))
    instead of using eval

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>