4 class RaiseError #:nodoc:
5 def initialize(error_or_message=Exception, message=nil)
6 if String === error_or_message
7 @expected_error = Exception
8 @expected_message = error_or_message
10 @expected_error = error_or_message
11 @expected_message = message
16 @raised_expected_error = false
20 rescue @expected_error => @actual_error
21 if @expected_message.nil?
22 @raised_expected_error = true
24 case @expected_message
26 if @expected_message =~ @actual_error.message
27 @raised_expected_error = true
32 if @expected_message == @actual_error.message
33 @raised_expected_error = true
39 rescue => @actual_error
42 return @raised_expected_error
47 return "expected #{expected_error}#{actual_error}" if @raised_other || !@raised_expected_error
50 def negative_failure_message
51 "expected no #{expected_error}#{actual_error}"
55 "raise #{expected_error}"
60 case @expected_message
64 "#{@expected_error} with message matching #{@expected_message.inspect}"
66 "#{@expected_error} with #{@expected_message.inspect}"
71 @actual_error.nil? ? " but nothing was raised" : ", got #{@actual_error.inspect}"
76 # should raise_error()
77 # should raise_error(NamedError)
78 # should raise_error(NamedError, String)
79 # should raise_error(NamedError, Regexp)
80 # should_not raise_error()
81 # should_not raise_error(NamedError)
82 # should_not raise_error(NamedError, String)
83 # should_not raise_error(NamedError, Regexp)
85 # With no args, matches if any error is raised.
86 # With a named error, matches only if that specific error is raised.
87 # With a named error and messsage specified as a String, matches only if both match.
88 # With a named error and messsage specified as a Regexp, matches only if both match.
92 # lambda { do_something_risky }.should raise_error
93 # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError)
94 # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, "that was too risky")
95 # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, /oo ri/)
97 # lambda { do_something_risky }.should_not raise_error
98 # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError)
99 # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, "that was too risky")
100 # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, /oo ri/)
101 def raise_error(error=Exception, message=nil)
102 Matchers::RaiseError.new(error, message)