From 82c6be380ef80636c9986cada50c6b7aaec5acac Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 10 Nov 2008 14:15:40 -0500 Subject: [PATCH] Raise Webrat::PageLoadError when a failure occurs so that application exceptions can be more accurately tested (Ryan Briones) --- History.txt | 6 ++++++ lib/webrat.rb | 3 +++ lib/webrat/core/session.rb | 6 ++++-- spec/webrat/core/session_spec.rb | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/History.txt b/History.txt index 505f58d..b2d78cf 100644 --- a/History.txt +++ b/History.txt @@ -1,3 +1,9 @@ +== Trunk + +* Minor enhancements + + * Raise Webrat::PageLoadError when a failure occurs so that application exceptions can be more accurately tested (Ryan Briones) + == 0.3.2 / 2008-11-08 * Minor enhancements diff --git a/lib/webrat.rb b/lib/webrat.rb index 926e8ae..364dbf1 100644 --- a/lib/webrat.rb +++ b/lib/webrat.rb @@ -8,6 +8,9 @@ module Webrat def self.root #:nodoc: defined?(RAILS_ROOT) ? RAILS_ROOT : Merb.root end + + class WebratError < StandardError + end end # We need Nokogiri's CSS to XPath support, even if using REXML diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 32a5840..2a9f5dd 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -4,10 +4,12 @@ require "ostruct" require "webrat/core/mime" module Webrat + class PageLoadError < WebratError + end + class Session extend Forwardable include Logging - include Flunk attr_reader :current_url @@ -85,7 +87,7 @@ module Webrat end save_and_open_page if exception_caught? - flunk("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 @_page_scope = nil diff --git a/spec/webrat/core/session_spec.rb b/spec/webrat/core/session_spec.rb index 37136de..fc7c9df 100644 --- a/spec/webrat/core/session_spec.rb +++ b/spec/webrat/core/session_spec.rb @@ -69,5 +69,20 @@ describe Webrat::Session do lambda { @session.http_accept(:oogabooga) }.should raise_error(ArgumentError) end end + + describe "#request_page" do + before(:each) do + @session = Webrat::Session.new + end + it "should raise an error if the request is not a success" 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.stub!(:save_and_open_page) + + lambda { @session.request_page('some url', :get, {}) }.should raise_error(Webrat::PageLoadError) + end + end end