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:
parent
00433bafe5
commit
7d7c32256d
|
@ -1,30 +1,52 @@
|
||||||
require 'webrat/rack'
|
require "webrat/rack"
|
||||||
require 'sinatra'
|
require "sinatra/test"
|
||||||
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
|
|
||||||
|
|
||||||
module Webrat
|
module Webrat
|
||||||
class SinatraSession < RackSession #:nodoc:
|
class SinatraSession < RackSession
|
||||||
include Sinatra::Test
|
include Sinatra::Test
|
||||||
|
|
||||||
attr_reader :request, :response
|
attr_reader :request, :response
|
||||||
|
|
||||||
%w(get head post put delete).each do |verb|
|
def initialize(context = nil)
|
||||||
alias_method "orig_#{verb}", verb
|
super(context)
|
||||||
define_method(verb) do |*args| # (path, data, headers = nil)
|
|
||||||
path, data, headers = *args
|
app =
|
||||||
data = data.inject({}) {|data, (key,value)| data[key] = Rack::Utils.unescape(value); data }
|
if context.respond_to?(:app)
|
||||||
params = data.merge(:env => headers || {})
|
context.app
|
||||||
self.__send__("orig_#{verb}", path, params)
|
else
|
||||||
end
|
Sinatra::Application.tap { |app|
|
||||||
|
app.set :environment, :test
|
||||||
|
app.disable :run, :reload
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
@browser = Sinatra::TestHarness.new(app)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -61,4 +61,4 @@ __END__
|
||||||
|
|
||||||
@@ hello
|
@@ hello
|
||||||
<p>Hello, <%= @user %></p>
|
<p>Hello, <%= @user %></p>
|
||||||
<p>Your email is: <%= @email %></p>
|
<p>Your email is: <%= @email %></p>
|
|
@ -0,0 +1,8 @@
|
||||||
|
require "rubygems"
|
||||||
|
require "sinatra/base"
|
||||||
|
|
||||||
|
class MyModularApp < Sinatra::Default
|
||||||
|
get "/" do
|
||||||
|
"Hello World"
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,6 +1,7 @@
|
||||||
require File.dirname(__FILE__) + "/test_helper"
|
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
|
def test_visits_pages
|
||||||
visit "/"
|
visit "/"
|
||||||
assert response_body.include?("visit")
|
assert response_body.include?("visit")
|
||||||
|
@ -23,7 +24,7 @@ class WebratTest < Test::Unit::TestCase
|
||||||
visit "/"
|
visit "/"
|
||||||
assert field_labeled("Prefilled").value, "text"
|
assert field_labeled("Prefilled").value, "text"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_follows_internal_redirects
|
def test_follows_internal_redirects
|
||||||
visit "/internal_redirect"
|
visit "/internal_redirect"
|
||||||
assert response_body.include?("visit")
|
assert response_body.include?("visit")
|
|
@ -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
|
|
@ -1,8 +1,6 @@
|
||||||
require "rubygems"
|
require "rubygems"
|
||||||
require "test/unit"
|
require "test/unit"
|
||||||
require "redgreen"
|
require "redgreen"
|
||||||
require "sinatra"
|
|
||||||
require File.dirname(__FILE__) + "/../app"
|
|
||||||
|
|
||||||
require File.dirname(__FILE__) + "/../../../../lib/webrat"
|
require File.dirname(__FILE__) + "/../../../../lib/webrat"
|
||||||
|
|
||||||
|
@ -12,6 +10,7 @@ end
|
||||||
|
|
||||||
class Test::Unit::TestCase
|
class Test::Unit::TestCase
|
||||||
include Webrat::Methods
|
include Webrat::Methods
|
||||||
|
include Webrat::Matchers
|
||||||
|
|
||||||
Webrat::Methods.delegate_to_session :response_code, :response_body
|
Webrat::Methods.delegate_to_session :response_code, :response_body
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
|
||||||
require "webrat/sinatra"
|
|
|
@ -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
|
|
Loading…
Reference in New Issue