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
Follow me on Twitter
My github repository
My LinkedIn Profile
WaYdotNET
Great post! Many thanks !
Feb 24, 2011 @ 3:25 am
nash
You can use this
Capybara.app, _ = Rack::Builder.parse_file(File.expand_path(‘../../config.ru’, __FILE__))
instead of using eval
Nov 03, 2011 @ 3:04 am