Skip to content

Commit 7a19a59

Browse files
committed
Fix raise_error to not swallow any non-matching exception in combination with should_not!
* lambda { ... }.should_not raise_error(SomeError) should rethrow if any non-matching exception is thrown by the block, otherwise the matcher silently swallows it and returns false, which #should_not interprets as a success!
1 parent 968fab4 commit 7a19a59

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

lib/mspec/matchers/raise_error.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,26 @@ def matches?(proc)
99
@result = proc.call
1010
return false
1111
rescue Exception => @actual
12-
return false unless @exception === @actual
12+
if matching_exception?(@actual)
13+
return true
14+
else
15+
raise @actual
16+
end
17+
end
18+
19+
def matching_exception?(exc)
20+
return false unless @exception === exc
1321
if @message then
1422
case @message
15-
when String then
16-
return false if @message != @actual.message
17-
when Regexp then
18-
return false if @message !~ @actual.message
23+
when String
24+
return false if @message != exc.message
25+
when Regexp
26+
return false if @message !~ exc.message
1927
end
2028
end
2129

22-
@block[@actual] if @block
30+
# The block has its own expectations and will throw an exception if it fails
31+
@block[exc] if @block
2332

2433
return true
2534
end

spec/matchers/raise_error_spec.rb

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class UnexpectedException < Exception; end
88
describe RaiseErrorMatcher do
99
it "matches when the proc raises the expected exception" do
1010
proc = Proc.new { raise ExpectedException }
11-
RaiseErrorMatcher.new(ExpectedException, nil).matches?(proc).should == true
11+
matcher = RaiseErrorMatcher.new(ExpectedException, nil)
12+
matcher.matches?(proc).should == true
1213
end
1314

1415
it "executes it's optional block if matched" do
@@ -25,28 +26,50 @@ class UnexpectedException < Exception; end
2526

2627
it "matches when the proc raises the expected exception with the expected message" do
2728
proc = Proc.new { raise ExpectedException, "message" }
28-
RaiseErrorMatcher.new(ExpectedException, "message").matches?(proc).should == true
29+
matcher = RaiseErrorMatcher.new(ExpectedException, "message")
30+
matcher.matches?(proc).should == true
31+
end
32+
33+
it "matches when the proc raises the expected exception with a matching message" do
34+
proc = Proc.new { raise ExpectedException, "some message" }
35+
matcher = RaiseErrorMatcher.new(ExpectedException, /some/)
36+
matcher.matches?(proc).should == true
2937
end
3038

3139
it "does not match when the proc does not raise the expected exception" do
32-
proc = Proc.new { raise UnexpectedException }
33-
RaiseErrorMatcher.new(ExpectedException, nil).matches?(proc).should == false
40+
exc = UnexpectedException.new
41+
matcher = RaiseErrorMatcher.new(ExpectedException, nil)
42+
43+
matcher.matching_exception?(exc).should == false
44+
lambda {
45+
matcher.matches?(Proc.new { raise exc })
46+
}.should raise_error(UnexpectedException)
3447
end
3548

3649
it "does not match when the proc raises the expected exception with an unexpected message" do
37-
proc = Proc.new { raise ExpectedException, "unexpected" }
38-
RaiseErrorMatcher.new(ExpectedException, "expected").matches?(proc).should == false
50+
exc = ExpectedException.new("unexpected")
51+
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
52+
53+
matcher.matching_exception?(exc).should == false
54+
lambda {
55+
matcher.matches?(Proc.new { raise exc })
56+
}.should raise_error(ExpectedException)
3957
end
4058

4159
it "does not match when the proc does not raise an exception" do
4260
proc = Proc.new {}
43-
RaiseErrorMatcher.new(ExpectedException, "expected").matches?(proc).should == false
61+
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
62+
matcher.matches?(proc).should == false
4463
end
4564

4665
it "provides a useful failure message" do
47-
proc = Proc.new { raise UnexpectedException, "unexpected" }
66+
exc = UnexpectedException.new("unexpected")
4867
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
49-
matcher.matches?(proc)
68+
69+
matcher.matching_exception?(exc).should == false
70+
lambda {
71+
matcher.matches?(Proc.new { raise exc })
72+
}.should raise_error(UnexpectedException)
5073
matcher.failure_message.should ==
5174
["Expected ExpectedException (expected)", "but got UnexpectedException (unexpected)"]
5275
end

0 commit comments

Comments
 (0)