1 require File.dirname(__FILE__) + '/../spec_helper'
2 require 'mspec/expectations/expectations'
3 require 'mspec/matchers/raise_error'
5 class ExpectedException < Exception; end
6 class UnexpectedException < Exception; end
8 describe RaiseErrorMatcher do
9 it "matches when the proc raises the expected exception" do
10 proc = Proc.new { raise ExpectedException }
11 RaiseErrorMatcher.new(ExpectedException, nil).matches?(proc).should == true
14 it "executes it's optional block if matched" do
16 proc = Proc.new { raise ExpectedException }
17 matcher = RaiseErrorMatcher.new(ExpectedException, nil) { |error|
19 error.class.should == ExpectedException
22 matcher.matches?(proc).should == true
26 it "matches when the proc raises the expected exception with the expected message" do
27 proc = Proc.new { raise ExpectedException, "message" }
28 RaiseErrorMatcher.new(ExpectedException, "message").matches?(proc).should == true
31 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
36 it "does not match when the proc raises the expected exception with an unexpected message" do
37 proc = Proc.new { raise UnexpectedException, "unexpected" }
38 RaiseErrorMatcher.new(ExpectedException, "expected").matches?(proc).should == false
41 it "provides a useful failure message" do
42 proc = Proc.new { raise UnexpectedException, "unexpected" }
43 matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
44 matcher.matches?(proc)
45 matcher.failure_message.should ==
46 ["Expected ExpectedException (expected)", "but got UnexpectedException (unexpected)"]
49 it "provides a useful negative failure message" do
50 proc = Proc.new { raise ExpectedException, "expected" }
51 matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
52 matcher.matches?(proc)
53 matcher.negative_failure_message.should ==
54 ["Expected to not get ExpectedException (expected)", ""]