Support for "modular" Sinatra app style

Since Sinatra 0.9, apps can be ancapsulated in a class ("modular" style)
or be on the top level, just like before ("classic" style)

* http://wiki.github.com/brynary/webrat/sinatra
* http://www.sinatrarb.com/extensions.html
This commit is contained in:
Simon Rozet 2009-02-22 01:07:15 +01:00
parent 00433bafe5
commit 7d7c32256d
8 changed files with 73 additions and 57 deletions

View File

@ -1,30 +1,52 @@
require 'webrat/rack'
require 'sinatra'
require 'sinatra/test'
class Sinatra::Application
# Override this to prevent Sinatra from barfing on the options passed from RSpec
def self.load_default_options_from_command_line!
end
end
disable :run
disable :reload
require "webrat/rack"
require "sinatra/test"
module Webrat
class SinatraSession < RackSession #:nodoc:
class SinatraSession < RackSession
include Sinatra::Test
attr_reader :request, :response
%w(get head post put delete).each do |verb|
alias_method "orig_#{verb}", verb
define_method(verb) do |*args| # (path, data, headers = nil)
path, data, headers = *args
data = data.inject({}) {|data, (key,value)| data[key] = Rack::Utils.unescape(value); data }
params = data.merge(:env => headers || {})
self.__send__("orig_#{verb}", path, params)
end
def initialize(context = nil)
super(context)
app =
if context.respond_to?(:app)
context.app
else
Sinatra::Application.tap { |app|
app.set :environment, :test
app.disable :run, :reload
}
end
@browser = Sinatra::TestHarness.new(app)
end
%w(get head post put delete).each do |verb|
class_eval <<-RUBY
def #{verb}(path, data, headers = {})
params = data.inject({}) do |data, (key,value)|
data[key] = Rack::Utils.unescape(value)
data
end
@browser.#{verb}(path, params, headers)
end
RUBY
end
def response_body
@browser.body
end
def response_code
@browser.status
end
private
def response
@browser.response
end
end
end

View File

@ -0,0 +1,8 @@
require "rubygems"
require "sinatra/base"
class MyModularApp < Sinatra::Default
get "/" do
"Hello World"
end
end

View File

@ -1,6 +1,7 @@
require File.dirname(__FILE__) + "/test_helper"
require File.dirname(__FILE__) + "/../classic_app"
class WebratTest < Test::Unit::TestCase
class MyClassicAppTest < Test::Unit::TestCase
def test_visits_pages
visit "/"
assert response_body.include?("visit")

View File

@ -0,0 +1,16 @@
require File.dirname(__FILE__) + "/test_helper"
require File.dirname(__FILE__) + "/../modular_app"
class MyModularAppTest < Test::Unit::TestCase
def app
MyModularApp.tap { |app|
app.disable :run, :reload
app.set :environment, :test
}
end
def test_it_works
visit "/"
assert_contain "Hello World"
end
end

View File

@ -1,8 +1,6 @@
require "rubygems"
require "test/unit"
require "redgreen"
require "sinatra"
require File.dirname(__FILE__) + "/../app"
require File.dirname(__FILE__) + "/../../../../lib/webrat"
@ -12,6 +10,7 @@ end
class Test::Unit::TestCase
include Webrat::Methods
include Webrat::Matchers
Webrat::Methods.delegate_to_session :response_code, :response_body
end

View File

@ -1,2 +0,0 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require "webrat/sinatra"

View File

@ -1,28 +0,0 @@
require File.expand_path(File.dirname(__FILE__) + '/helper')
describe Webrat::SinatraSession, "API" do
before :each do
Webrat.configuration.mode = :sinatra
@sinatra_session = Webrat::SinatraSession.new
end
it "should delegate get to sinatras get" do
@sinatra_session.should_receive(:orig_get).with("url", { :env => "headers" })
@sinatra_session.get("url", {}, "headers")
end
it "should delegate post to sinatras post" do
@sinatra_session.should_receive(:orig_post).with("url", { :env => "headers" })
@sinatra_session.post("url", {}, "headers")
end
it "should delegate put to sinatras put" do
@sinatra_session.should_receive(:orig_put).with("url", { :env => "headers" })
@sinatra_session.put("url", {}, "headers")
end
it "should delegate delete to sinatras delete" do
@sinatra_session.should_receive(:orig_delete).with("url", { :env => "headers" })
@sinatra_session.delete("url", {}, "headers")
end
end