From 7460c85655fa4c6682f8e7b286f02a773b4c44c4 Mon Sep 17 00:00:00 2001 From: gaffo Date: Thu, 13 Nov 2008 23:58:52 -0600 Subject: [PATCH] [#33 state:resolved] added a configuration object that is configurable by a block --- lib/webrat/core.rb | 1 + lib/webrat/core/configuration.rb | 33 ++++++++++++++++++++++++++ lib/webrat/core/session.rb | 2 +- spec/spec_helper.rb | 13 ++++++++++ spec/webrat/core/configuration_spec.rb | 30 +++++++++++++++++++++++ spec/webrat/core/session_spec.rb | 14 +++++++++++ 6 files changed, 92 insertions(+), 1 deletion(-) create mode 100755 lib/webrat/core/configuration.rb create mode 100755 spec/webrat/core/configuration_spec.rb diff --git a/lib/webrat/core.rb b/lib/webrat/core.rb index cce4704..1c4d29a 100644 --- a/lib/webrat/core.rb +++ b/lib/webrat/core.rb @@ -1,3 +1,4 @@ +require "webrat/core/configuration" require "webrat/core/nokogiri" require "webrat/core/logging" require "webrat/core/flunk" diff --git a/lib/webrat/core/configuration.rb b/lib/webrat/core/configuration.rb new file mode 100755 index 0000000..722fc14 --- /dev/null +++ b/lib/webrat/core/configuration.rb @@ -0,0 +1,33 @@ +module Webrat + module Core + class Configuration + + # 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 + + def self.configuration + @@configuration = Webrat::Core::Configuration.new unless @@configuration + @@configuration + end + + # 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 + @@configuration = nil + + def default_open_error_files + true + end + + end + end +end \ No newline at end of file diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 2a9f5dd..4c09871 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -86,7 +86,7 @@ module Webrat send "#{http_method}", url, data || {}, h end - save_and_open_page if exception_caught? + save_and_open_page if exception_caught? && !Webrat::Core::Configuration.configuration.open_error_files raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code? @_scopes = nil diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 53e5896..be1c229 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,4 +9,17 @@ require File.expand_path(File.dirname(__FILE__) + "/fakes/test_session") Spec::Runner.configure do |config| # Nothing to configure yet +end + + +class Webrat::Core::Configuration + @@previous_config = nil + + def self.cache_config_for_test + @@configuration = Webrat::Core::Configuration.configuration + end + + def self.reset_for_test + @@configuration = @@previous_config if @@previous_config + end end \ No newline at end of file diff --git a/spec/webrat/core/configuration_spec.rb b/spec/webrat/core/configuration_spec.rb new file mode 100755 index 0000000..d98bd04 --- /dev/null +++ b/spec/webrat/core/configuration_spec.rb @@ -0,0 +1,30 @@ +require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") + +describe Webrat::Core::Configuration do + + before do + Webrat::Core::Configuration.cache_config_for_test + end + + after do + Webrat::Core::Configuration.reset_for_test + end + + it "should have a default config" do + Webrat::Core::Configuration.configuration.should be_an_instance_of(Webrat::Core::Configuration) + end + + it "should set default values" do + config = Webrat::Core::Configuration.configuration + config.open_error_files.should == true + end + + it "should be configurable with a block" do + Webrat::Core::Configuration.configure do |config| + config.open_error_files = false + end + config = Webrat::Core::Configuration.configuration + config.open_error_files.should == false + end + +end \ No newline at end of file diff --git a/spec/webrat/core/session_spec.rb b/spec/webrat/core/session_spec.rb index fc7c9df..b43efcf 100644 --- a/spec/webrat/core/session_spec.rb +++ b/spec/webrat/core/session_spec.rb @@ -72,8 +72,12 @@ describe Webrat::Session do describe "#request_page" do before(:each) do + Webrat::Core::Configuration.cache_config_for_test @session = Webrat::Session.new end + after(:each) do + Webrat::Core::Configuration.reset_for_test + end it "should raise an error if the request is not a success" do @session.stub!(:get) @@ -84,5 +88,15 @@ describe Webrat::Session do lambda { @session.request_page('some url', :get, {}) }.should raise_error(Webrat::PageLoadError) end + + it "should raise an error but not open if the request is not a success and config quashes save_and_open" do + @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