Merge commit 'gaffo/webrat_config'

This commit is contained in:
Bryan Helmkamp 2008-11-16 18:51:42 -05:00
commit 9f8a88d649
7 changed files with 98 additions and 2 deletions

View File

@ -3,14 +3,31 @@ require "rubygems"
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__))) $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
module Webrat module Webrat
class WebratError < StandardError
end
VERSION = '0.3.2' VERSION = '0.3.2'
def self.root #:nodoc: def self.root #:nodoc:
defined?(RAILS_ROOT) ? RAILS_ROOT : Merb.root defined?(RAILS_ROOT) ? RAILS_ROOT : Merb.root
end end
class WebratError < StandardError
# Configures Webrat. If this is not done, Webrat will be created
# with all of the default settings.
def self.configure(configuration = Webrat::Core::Configuration.new)
yield configuration if block_given?
@@configuration = configuration
end end
def self.configuration
@@configuration = Webrat::Core::Configuration.new unless @@configuration
@@configuration
end
private
@@configuration = nil
end end
# We need Nokogiri's CSS to XPath support, even if using REXML and Hpricot for parsing and searching # We need Nokogiri's CSS to XPath support, even if using REXML and Hpricot for parsing and searching

View File

@ -1,3 +1,4 @@
require "webrat/core/configuration"
require "webrat/core/xml" require "webrat/core/xml"
require "webrat/core/nokogiri" require "webrat/core/nokogiri"
require "webrat/core/logging" require "webrat/core/logging"

View File

@ -0,0 +1,18 @@
module Webrat
module Core
class Configuration
# Sets whether to save and open pages with error status codes in a browser
attr_accessor :open_error_files
def initialize
self.open_error_files = default_open_error_files
end
private
def default_open_error_files
true
end
end
end
end

View File

@ -86,7 +86,7 @@ module Webrat
send "#{http_method}", url, data || {}, h send "#{http_method}", url, data || {}, h
end end
save_and_open_page if exception_caught? save_and_open_page if exception_caught? && Webrat.configuration.open_error_files
raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code? raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?
@_scopes = nil @_scopes = nil

View File

@ -10,3 +10,16 @@ require File.expand_path(File.dirname(__FILE__) + "/fakes/test_session")
Spec::Runner.configure do |config| Spec::Runner.configure do |config|
# Nothing to configure yet # Nothing to configure yet
end end
module Webrat
@@previous_config = nil
def self.cache_config_for_test
@@configuration = Webrat.configuration
end
def self.reset_for_test
@@configuration = @@previous_config if @@previous_config
end
end

View File

@ -0,0 +1,30 @@
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
describe Webrat::Core::Configuration do
before do
Webrat.cache_config_for_test
end
after do
Webrat.reset_for_test
end
it "should have a default config" do
Webrat.configuration.should be_an_instance_of(Webrat::Core::Configuration)
end
it "should set default values" do
config = Webrat.configuration
config.open_error_files.should == true
end
it "should be configurable with a block" do
Webrat.configure do |config|
config.open_error_files = false
end
config = Webrat.configuration
config.open_error_files.should == false
end
end

View File

@ -87,8 +87,12 @@ describe Webrat::Session do
describe "#request_page" do describe "#request_page" do
before(:each) do before(:each) do
Webrat.cache_config_for_test
@session = Webrat::Session.new @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
@session.stub!(:get) @session.stub!(:get)
@ -99,5 +103,18 @@ describe Webrat::Session do
lambda { @session.request_page('some url', :get, {}) }.should raise_error(Webrat::PageLoadError) lambda { @session.request_page('some url', :get, {}) }.should raise_error(Webrat::PageLoadError)
end end
it "should raise an error but not open if the request is not a success and config quashes save_and_open" do
Webrat.configure do |config|
config.open_error_files = false
end
@session.stub!(:get)
@session.stub!(:response_body).and_return("Exception caught")
@session.stub!(:response_code).and_return(500)
@session.stub!(:formatted_error).and_return("application error")
@session.should_not_receive(:save_and_open_page)
lambda { @session.request_page('some url', :get, {}) }.should raise_error(Webrat::PageLoadError)
end
end end
end end