1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

MUCH LOVE for testing

This commit is contained in:
Blake Mizerany 2007-10-26 15:37:43 -07:00
parent 99326b2eeb
commit 96477afca5
7 changed files with 108 additions and 76 deletions

View file

@ -43,7 +43,7 @@ Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/vendor/*/init.rb')
Sinatra::Loader.load_files Dir.glob(File.dirname($0) + '/vendor/*/init.rb')
at_exit do
Sinatra::Environment.prepare_loggers
Sinatra::Environment.prepare_loggers unless Sinatra::Environment.test?
Sinatra::Irb.start! if Sinatra::Options.console
Sinatra::Server.new.start unless Sinatra::Server.running
end

View file

@ -1,6 +1,10 @@
module Sinatra
module Environment
extend self
def test?
Options.environment == :test
end
def prepare
Options.parse!(ARGV)

View file

@ -52,6 +52,11 @@ module Sinatra
self.before_filters = []
self.after_filters = []
def self.reset!
self.before_filters.clear
self.after_filters.clear
end
def self.before_attend(method_name = nil, options ={}, &block)
setup_filter(:before_filters, method_name, options, &block)
end
@ -70,9 +75,7 @@ module Sinatra
insert_index = options[:infront] == true ? 0 : -1
send(filter_set_name).insert(insert_index, value)
end
after_attend :log_event
attr_reader :path, :verb
def initialize(verb, path, register = true, &block)
@ -107,6 +110,7 @@ module Sinatra
context.body context.body || body || ''
call_filters(after_filters, context)
end
context.log_event
context
end
alias :call :attend

View file

@ -0,0 +1,59 @@
require 'uri'
module Sinatra
# These methods are for integration testing without an internet connection. They are available in Test::Unit::TestCase and when in Irb.
module Test
module Methods
# get_it, post_it, put_it, delete_it
# Executes the method and returns the result of the body
#
# options:
# +:params+ a hash of name parameters
#
# Example:
# get_it '/', :name => 'Blake' # => 'Hello Blake!'
#
%w(get post put delete).each do |verb|
module_eval <<-end_eval
def #{verb}_it(path, params = {})
request = Rack::MockRequest.new(Sinatra::Dispatcher.new)
@response = request.#{verb} path, :input => generate_input(params)
body
end
end_eval
end
def response
@response || Rack::MockResponse.new(404, {}, '')
end
def status
response.status
end
def text
response.body
end
alias :xml :text
alias :html :text
alias :body :text
def headers
response.headers
end
private
def generate_input(params)
params.map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&')
end
end
end
end

35
lib/sinatra/test/spec.rb Normal file
View file

@ -0,0 +1,35 @@
require File.dirname(__FILE__) + '/methods'
module Sinatra
module Test
module Spec
def self.included(base)
require File.dirname(__FILE__) + '/../../sinatra'
require 'test/spec'
Server.running = true
Options.set_environment :test
Environment.prepare_loggers
end
end
end
end
include Sinatra::Test::Spec
class Test::Spec::TestCase
module InstanceMethods
include Sinatra::Test::Methods
end
alias :initialize_orig :initialize
def initialize(name, parent=nil, superclass=Test::Unit::TestCase)
initialize_orig(name, parent, superclass)
@testcase.setup do
Sinatra::EventManager.reset!
Sinatra::Event.reset!
end
end
end

View file

@ -1,55 +0,0 @@
require 'uri'
module Sinatra
# These methods are for integration testing without an internet connection. They are available in Test::Unit::TestCase and when in Irb.
module TestMethods
# get_it, post_it, put_it, delete_it
# Executes the method and returns the result of the body
#
# options:
# +:params+ a hash of name parameters
#
# Example:
# get_it '/', :name => 'Blake' # => 'Hello Blake!'
#
%w(get post put delete).each do |verb|
module_eval <<-end_eval
def #{verb}_it(path, params = {})
request = Rack::MockRequest.new(Sinatra::Dispatcher.new)
@response = request.#{verb} path, :input => generate_input(params)
body
end
end_eval
end
def response
@response || Rack::MockResponse.new(404, {}, '')
end
def status
response.status
end
def text
response.body
end
alias :xml :text
alias :html :text
alias :body :text
def headers
response.headers
end
private
def generate_input(params)
params.map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&')
end
end
end

View file

@ -1,17 +1,2 @@
require File.dirname(__FILE__) + '/../lib/sinatra'
%w(mocha test/spec).each do |library|
begin
require library
rescue
STDERR.puts "== Sinatra's tests need #{library} to run."
end
end
Sinatra::Server.running = true
Sinatra::Options.set_environment :test
Sinatra::Environment.prepare_loggers
class Test::Unit::TestCase
include Sinatra::TestMethods
end
require File.dirname(__FILE__) + '/../lib/sinatra/test/spec'
require 'mocha'