Skip to content

Commit

Permalink
Raise Webrat::PageLoadError when a failure occurs so that application…
Browse files Browse the repository at this point in the history
… exceptions can be more accurately tested (Ryan Briones)
  • Loading branch information
brynary committed Nov 10, 2008
1 parent 59704da commit 82c6be3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions 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
Expand Down
3 changes: 3 additions & 0 deletions lib/webrat.rb
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/webrat/core/session.rb
Expand Up @@ -4,10 +4,12 @@
require "webrat/core/mime"

module Webrat
class PageLoadError < WebratError
end

class Session
extend Forwardable
include Logging
include Flunk

attr_reader :current_url

Expand Down Expand Up @@ -85,7 +87,7 @@ def request_page(url, http_method, data) #:nodoc:
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
Expand Down
15 changes: 15 additions & 0 deletions spec/webrat/core/session_spec.rb
Expand Up @@ -69,5 +69,20 @@ def session.response_body
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

0 comments on commit 82c6be3

Please sign in to comment.