Re-enable spec/library for full CI runs.
[rbx.git] / mspec / spec / matchers / raise_error_spec.rb
blob39cfdc00b440b23ff8b7c32eabe3759326d1574d
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
12   end
14   it "executes it's optional block if matched" do
15     run = false
16     proc = Proc.new { raise ExpectedException }
17     matcher = RaiseErrorMatcher.new(ExpectedException, nil) { |error|
18       run = true
19       error.class.should == ExpectedException
20     }
22     matcher.matches?(proc).should == true
23     run.should == true
24   end
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
29   end
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
34   end
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
39   end
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)"]
47   end
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)", ""]
55   end
56 end