Cleanup the way the tests switch Webrat modes

This commit is contained in:
Bryan Helmkamp 2008-12-25 23:25:27 -05:00
parent 22c7834512
commit 0eaad52768
11 changed files with 41 additions and 59 deletions

View File

@ -1,4 +1,6 @@
require "webrat" require "webrat"
require "action_controller"
require "action_controller/integration" require "action_controller/integration"
module Webrat module Webrat

View File

@ -7,27 +7,9 @@ begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end
webrat_path = File.expand_path(File.dirname(__FILE__) + "/../lib/") webrat_path = File.expand_path(File.dirname(__FILE__) + "/../lib/")
$LOAD_PATH.unshift(webrat_path) unless $LOAD_PATH.include?(webrat_path) $LOAD_PATH.unshift(webrat_path) unless $LOAD_PATH.include?(webrat_path)
require "merb-core"
require "webrat/merb"
require "webrat" require "webrat"
require File.expand_path(File.dirname(__FILE__) + "/fakes/test_session") require File.expand_path(File.dirname(__FILE__) + "/fakes/test_session")
Spec::Runner.configure do |config|
include Webrat::Methods
def with_html(html)
raise "This doesn't look like HTML. Wrap it in a <html> tag" unless html =~ /^\s*<[^Hh>]*html/i
webrat_session.response_body = html
end
def with_xml(xml)
raise "This looks like HTML" if xml =~ /^\s*<[^Hh>]*html/i
webrat_session.response_body = xml
end
end
module Webrat module Webrat
@@previous_config = nil @@previous_config = nil
@ -38,10 +20,30 @@ module Webrat
def self.reset_for_test def self.reset_for_test
@@configuration = @@previous_config if @@previous_config @@configuration = @@previous_config if @@previous_config
end end
end
Spec::Runner.configure do |config|
include Webrat::Methods
class Configuration def with_html(html)
def mode_for_test= (mode) raise "This doesn't look like HTML. Wrap it in a <html> tag" unless html =~ /^\s*<[^Hh>]*html/i
@mode = mode webrat_session.response_body = html
end
end end
end
def with_xml(xml)
raise "This looks like HTML" if xml =~ /^\s*<[^Hh>]*html/i
webrat_session.response_body = xml
end
config.before :each do
Webrat.cache_config_for_test
end
config.after :each do
Webrat.reset_for_test
end
end
Webrat.configure do |config|
config.mode = :merb
end

View File

@ -4,14 +4,6 @@ describe Webrat::Configuration do
predicate_matchers[:parse_with_nokogiri] = :parse_with_nokogiri? predicate_matchers[:parse_with_nokogiri] = :parse_with_nokogiri?
predicate_matchers[:open_error_files] = :open_error_files? predicate_matchers[:open_error_files] = :open_error_files?
before do
Webrat.cache_config_for_test
end
after do
Webrat.reset_for_test
end
it "should have a mode" do it "should have a mode" do
Webrat.configuration.should respond_to(:mode) Webrat.configuration.should respond_to(:mode)
end end

View File

@ -87,12 +87,8 @@ describe Webrat::Session do
describe "#request_page" do describe "#request_page" do
before(:each) do before(:each) do
Webrat.cache_config_for_test
webrat_session = Webrat::Session.new webrat_session = Webrat::Session.new
end end
after(:each) do
Webrat.reset_for_test
end
it "should raise an error if the request is not a success" do it "should raise an error if the request is not a success" do
webrat_session.stub!(:get) webrat_session.stub!(:get)

View File

@ -1,14 +1,11 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
describe "Webrat::MechanizeSession" do require "webrat/mechanize"
before :all do
Webrat.cache_config_for_test describe Webrat::MechanizeSession do
before :each do
Webrat.configuration.mode = :mechanize Webrat.configuration.mode = :mechanize
end end
after :all do
Webrat.reset_for_test
end
before(:each) do before(:each) do
@mech = Webrat::MechanizeSession.new @mech = Webrat::MechanizeSession.new

View File

@ -1,2 +0,0 @@
require 'merb-core'
require "webrat/merb"

View File

@ -1,5 +1,4 @@
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
require File.expand_path(File.dirname(__FILE__) + "/helper")
describe HashWithIndifferentAccess do describe HashWithIndifferentAccess do
it "should not update constructor when not a hash" do it "should not update constructor when not a hash" do

View File

@ -2,8 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
require "webrat/merb" require "webrat/merb"
describe Webrat::Session do describe Webrat::MerbSession do
it "should not pass empty params if data is and empty hash" do it "should not pass empty params if data is and empty hash" do
session = Webrat::MerbSession.new session = Webrat::MerbSession.new
response = OpenStruct.new response = OpenStruct.new

View File

@ -1,7 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + '/helper') require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
describe "attach_file" do describe "attach_file" do
before do before do
Webrat.configuration.mode = :rails
@filename = __FILE__ @filename = __FILE__
@uploaded_file = mock("uploaded file") @uploaded_file = mock("uploaded file")
ActionController::TestUploadedFile.stub!(:new => @uploaded_file) ActionController::TestUploadedFile.stub!(:new => @uploaded_file)

View File

@ -1,10 +0,0 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require "active_support"
silence_warnings do
require "action_controller"
require "action_controller/integration"
end
require "webrat/rails"
Webrat.configuration.mode_for_test = :rails

View File

@ -1,6 +1,12 @@
require File.expand_path(File.dirname(__FILE__) + '/helper') require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require "webrat/rails"
describe Webrat::RailsSession do describe Webrat::RailsSession do
before do
Webrat.configuration.mode = :rails
end
it "should delegate response_body to the session response body" do it "should delegate response_body to the session response body" do
response = mock("response", :body => "<html>") response = mock("response", :body => "<html>")
integration_session = mock("integration session", :response => response) integration_session = mock("integration session", :response => response)