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 + + + sinatra testing with webrat + + <%= yield %> + + + +@@ home +

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?('
') + end + + def test_submits_form + visit "/go" + fill_in "Name", :with => "World" + click_button "Submit" + + assert response_body.include?("Hello, World") + end +end diff --git a/spec/private/sinatra/helper.rb b/spec/private/sinatra/helper.rb index ce5417e..3d52256 100644 --- a/spec/private/sinatra/helper.rb +++ b/spec/private/sinatra/helper.rb @@ -1,15 +1,2 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') -require "webrat/sinatra" - -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 - -Sinatra::Application.default_options.merge!( - :env => :test, - :run => false, - :raise_errors => true, - :logging => false -) \ No newline at end of file +require "webrat/sinatra" \ No newline at end of file diff --git a/spec/private/sinatra/sinatra_spec.rb b/spec/private/sinatra/sinatra_session_spec.rb similarity index 60% rename from spec/private/sinatra/sinatra_spec.rb rename to spec/private/sinatra/sinatra_session_spec.rb index 73ab674..d493aa0 100644 --- a/spec/private/sinatra/sinatra_spec.rb +++ b/spec/private/sinatra/sinatra_session_spec.rb @@ -1,9 +1,11 @@ require File.expand_path(File.dirname(__FILE__) + '/helper') -describe Webrat::SinatraSession do +describe Webrat::SinatraSession, "API" do before :each do Webrat.configuration.mode = :sinatra @sinatra_session = Webrat::SinatraSession.new + @response = mock("response", :redirect? => false) + @sinatra_session.stub!(:response => @response) end it "should delegate get to get_it" do @@ -25,4 +27,14 @@ describe Webrat::SinatraSession do @sinatra_session.should_receive(:delete_it).with("url", { :env => "headers" }) @sinatra_session.delete("url", {}, "headers") end + + it "should use Session#request_page to handle redirects" do + @response.should_receive(:redirect?).twice.and_return(true, false) + @response.should_receive(:location).and_return("redirect url") + + @sinatra_session.should_receive(:get_it).with("original url", { :env => "headers" }) + @sinatra_session.should_receive(:request_page).with("redirect url", :get, {}) + + @sinatra_session.get("original url", {}, "headers") + end end \ No newline at end of file