diff --git a/Rakefile b/Rakefile index 5355606..3a147ba 100644 --- a/Rakefile +++ b/Rakefile @@ -105,14 +105,14 @@ end namespace :spec do desc "Run the integration specs" - task :integration => ["integration:rails", "integration:merb"] + task :integration => ["integration:rails", "integration:merb", "integration:sinatra"] namespace :integration do desc "Run the Rails integration specs" task :rails do Dir.chdir "spec/integration/rails" do result = system "rake test:integration" - raise "Tests failed" unless result + raise "Rails integration tests failed" unless result end end @@ -120,7 +120,15 @@ namespace :spec do task :merb do Dir.chdir "spec/integration/merb" do result = system "rake spec" - raise "Tests failed" unless result + raise "Merb integration tests failed" unless result + end + end + + desc "Run the Sinatra integration specs" + task :sinatra do + Dir.chdir "spec/integration/sinatra" do + result = system "rake test" + raise "Sinatra tntegration tests failed" unless result end end end diff --git a/lib/webrat/sinatra.rb b/lib/webrat/sinatra.rb index 30e41a5..8cf82df 100644 --- a/lib/webrat/sinatra.rb +++ b/lib/webrat/sinatra.rb @@ -2,15 +2,27 @@ require 'webrat/rack' require 'sinatra' require 'sinatra/test/methods' +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 class SinatraSession < RackSession #:nodoc: include Sinatra::Test::Methods + attr_reader :request, :response + %w(get head post put delete).each do |verb| define_method(verb) do |*args| # (path, data, headers = nil) path, data, headers = *args - params = data.merge({:env => headers || {}}) + params = data.merge(:env => headers || {}) self.__send__("#{verb}_it", path, params) + request_page(response.location, :get, {}) while response.redirect? end end end diff --git a/spec/integration/sinatra/Rakefile b/spec/integration/sinatra/Rakefile new file mode 100644 index 0000000..cfba77d --- /dev/null +++ b/spec/integration/sinatra/Rakefile @@ -0,0 +1,5 @@ +require "rake/testtask" + +Rake::TestTask.new do |t| + t.test_files = FileList["test/*_test.rb"] +end diff --git a/spec/integration/sinatra/app.rb b/spec/integration/sinatra/app.rb new file mode 100644 index 0000000..4235ebf --- /dev/null +++ b/spec/integration/sinatra/app.rb @@ -0,0 +1,41 @@ +require "rubygems" +require "sinatra" + +use_in_file_templates! + +get "/" do + erb :home +end + +get "/go" do + erb :go +end + +post "/go" do + @user = params[:name] + erb :hello +end + +__END__ + +@@ layout + + +
visit there
+ +@@ go + + +@@ hello +Hello, <%= @user %>
\ No newline at end of file diff --git a/spec/integration/sinatra/test/test_helper.rb b/spec/integration/sinatra/test/test_helper.rb new file mode 100644 index 0000000..c01c9b2 --- /dev/null +++ b/spec/integration/sinatra/test/test_helper.rb @@ -0,0 +1,17 @@ +require "rubygems" +require "test/unit" +require "redgreen" +require "sinatra" +require File.dirname(__FILE__) + "/../app" + +require File.dirname(__FILE__) + "/../../../../lib/webrat" + +Webrat.configure do |config| + config.mode = :sinatra +end + +class Test::Unit::TestCase + include Webrat::Methods + + Webrat::Methods.delegate_to_session :response_code, :response_body +end diff --git a/spec/integration/sinatra/test/webrat_test.rb b/spec/integration/sinatra/test/webrat_test.rb new file mode 100644 index 0000000..fe198f8 --- /dev/null +++ b/spec/integration/sinatra/test/webrat_test.rb @@ -0,0 +1,19 @@ +require File.dirname(__FILE__) + "/test_helper" + +class WebratTest < Test::Unit::TestCase + def test_visits_pages + visit "/" + assert response_body.include?("visit") + + click_link "there" + assert response_body.include?('