Permalink
Browse files

`to_raise()` now accepts exception instance or a string as argument i…

…n addition to exception class
  • Loading branch information...
Bartosz Blimke
Bartosz Blimke committed Jun 18, 2010
1 parent 436f7f5 commit 207443ef3002b252063e45e8703afdd9a8b6c80c
Showing with 51 additions and 5 deletions.
  1. +10 −0 README.md
  2. +13 −5 lib/webmock/response.rb
  3. +14 −0 spec/response_spec.rb
  4. +14 −0 spec/webmock_spec.rb
View
@@ -179,9 +179,19 @@ You can also use WebMock without RSpec or Test::Unit support:
### Raising errors
+#### Exception declared by class
+
stub_request(:any, 'www.example.net').to_raise(StandardError)
RestClient.post('www.example.net', 'abc') # ===> StandardError
+
+#### or by exception instance
+
+ stub_request(:any, 'www.example.net').to_raise(StandardError.new("some error"))
+
+#### or by string
+
+ stub_request(:any, 'www.example.net').to_raise("some error")
### Raising timeout errors
View
@@ -14,7 +14,7 @@ def self.response_for(options)
end
end
end
-
+
class Response
def initialize(options = {})
if options.is_a?(IO) || options.is_a?(String)
@@ -27,7 +27,7 @@ def initialize(options = {})
def headers
@headers
end
-
+
def headers=(headers)
@headers = headers
if @headers && !@headers.is_a?(Proc)
@@ -38,7 +38,7 @@ def headers=(headers)
def body
@body || ''
end
-
+
def body=(body)
@body = body
stringify_body!
@@ -56,8 +56,16 @@ def exception
@exception
end
+ def exception=(exception)
+ @exception = case exception
+ when String then StandardError.new(exception)
+ when Class then exception.new('Exception from WebMock')
+ when Exception then exception
+ end
+ end
+
def raise_error_if_any
- raise @exception.new('Exception from WebMock') if @exception
+ raise @exception if @exception
end
def should_timeout
@@ -68,7 +76,7 @@ def options=(options)
self.headers = options[:headers]
self.status = options[:status]
self.body = options[:body]
- @exception = options[:exception]
+ self.exception = options[:exception]
@should_timeout = options[:should_timeout]
end
View
@@ -58,6 +58,20 @@
@response.raise_error_if_any
}.should raise_error(ArgumentError, "Exception from WebMock")
end
+
+ it "should raise error if any assigned as instance" do
+ @response = Response.new(:exception => ArgumentError.new("hello world"))
+ lambda {
+ @response.raise_error_if_any
+ }.should raise_error(ArgumentError, "hello world")
+ end
+
+ it "should raise error if any assigned as string" do
+ @response = Response.new(:exception => "hello world")
+ lambda {
+ @response.raise_error_if_any
+ }.should raise_error("hello world")
+ end
it "should not raise error if no error assigned" do
@response.raise_error_if_any
View
@@ -351,6 +351,20 @@ class MyException < StandardError; end;
http_request(:get, "http://www.example.com/")
}.should raise_error(MyException, "Exception from WebMock")
end
+
+ it "should raise exception if declared in a stubbed response as exception instance" do
+ stub_http_request(:get, "www.example.com").to_raise(MyException.new("hello world"))
+ lambda {
+ http_request(:get, "http://www.example.com/")
+ }.should raise_error(MyException, "hello world")
+ end
+
+ it "should raise exception if declared in a stubbed response as exception instance" do
+ stub_http_request(:get, "www.example.com").to_raise("hello world")
+ lambda {
+ http_request(:get, "http://www.example.com/")
+ }.should raise_error("hello world")
+ end
it "should raise exception if declared in a stubbed response after returning declared response" do
stub_http_request(:get, "www.example.com").to_return(:body => "abc").then.to_raise(MyException)

0 comments on commit 207443e

Please sign in to comment.