1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
require 'spec_helper'
require 'mspec/expectations/expectations'
require 'mspec/matchers'
class ExpectedException < Exception; end
class UnexpectedException < Exception; end
describe RaiseErrorMatcher do
it "matches when the proc raises the expected exception" do
proc = Proc.new { raise ExpectedException }
matcher = RaiseErrorMatcher.new(ExpectedException, nil)
matcher.matches?(proc).should == true
end
it "executes it's optional block if matched" do
run = false
proc = Proc.new { raise ExpectedException }
matcher = RaiseErrorMatcher.new(ExpectedException, nil) { |error|
run = true
error.class.should == ExpectedException
}
matcher.matches?(proc).should == true
run.should == true
end
it "matches when the proc raises the expected exception with the expected message" do
proc = Proc.new { raise ExpectedException, "message" }
matcher = RaiseErrorMatcher.new(ExpectedException, "message")
matcher.matches?(proc).should == true
end
it "matches when the proc raises the expected exception with a matching message" do
proc = Proc.new { raise ExpectedException, "some message" }
matcher = RaiseErrorMatcher.new(ExpectedException, /some/)
matcher.matches?(proc).should == true
end
it "does not match when the proc does not raise the expected exception" do
exc = UnexpectedException.new
matcher = RaiseErrorMatcher.new(ExpectedException, nil)
matcher.matching_exception?(exc).should == false
lambda {
matcher.matches?(Proc.new { raise exc })
}.should raise_error(UnexpectedException)
end
it "does not match when the proc raises the expected exception with an unexpected message" do
exc = ExpectedException.new("unexpected")
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matching_exception?(exc).should == false
lambda {
matcher.matches?(Proc.new { raise exc })
}.should raise_error(ExpectedException)
end
it "does not match when the proc does not raise an exception" do
proc = Proc.new {}
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matches?(proc).should == false
end
it "provides a useful failure message" do
exc = UnexpectedException.new("unexpected")
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matching_exception?(exc).should == false
lambda {
matcher.matches?(Proc.new { raise exc })
}.should raise_error(UnexpectedException)
matcher.failure_message.should ==
["Expected ExpectedException (expected)", "but got UnexpectedException (unexpected)"]
end
it "provides a useful failure message when the proc raises the expected exception with an unexpected message" do
exc = ExpectedException.new("unexpected")
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matching_exception?(exc).should == false
lambda {
matcher.matches?(Proc.new { raise exc })
}.should raise_error(ExpectedException)
matcher.failure_message.should ==
["Expected ExpectedException (expected)", "but got ExpectedException (unexpected)"]
end
it "provides a useful failure message when no exception is raised" do
proc = Proc.new { 120 }
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matches?(proc)
matcher.failure_message.should ==
["Expected ExpectedException (expected)", "but no exception was raised (120 was returned)"]
end
it "provides a useful failure message when no exception is raised and nil is returned" do
proc = Proc.new { nil }
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matches?(proc)
matcher.failure_message.should ==
["Expected ExpectedException (expected)", "but no exception was raised (nil was returned)"]
end
it "provides a useful failure message when no exception is raised and the result raises in #pretty_inspect" do
result = Object.new
def result.pretty_inspect
raise ArgumentError, "bad"
end
proc = Proc.new { result }
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matches?(proc)
matcher.failure_message.should ==
["Expected ExpectedException (expected)", "but no exception was raised (#<Object>(#pretty_inspect raised #<ArgumentError: bad>) was returned)"]
end
it "provides a useful negative failure message" do
proc = Proc.new { raise ExpectedException, "expected" }
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matches?(proc)
matcher.negative_failure_message.should ==
["Expected to not get ExpectedException (expected)", ""]
end
it "provides a useful negative failure message for strict subclasses of the matched exception class" do
proc = Proc.new { raise UnexpectedException, "unexpected" }
matcher = RaiseErrorMatcher.new(Exception, nil)
matcher.matches?(proc)
matcher.negative_failure_message.should ==
["Expected to not get Exception", "but got UnexpectedException (unexpected)"]
end
end
|