Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In RSpec, raise_error and raise_exception are aliases. Since "error" is a more general term, it's reasonable that it matches "kinds of" exceptions. Here, raise_exception is used to match only a specific kind of exception. Any confusion that results will be short-lived as MSpec 2.0 will use a different syntax.
- Loading branch information
Showing
2 changed files
with
82 additions
and
50 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require 'spec_helper' | ||
require 'mspec/expectations/expectations' | ||
require 'mspec/matchers' | ||
|
||
class ExpectedException < Exception; end | ||
class UnexpectedException < Exception; end | ||
|
||
describe RaiseExceptionMatcher do | ||
it "matches when the proc raises the expected exception" do | ||
proc = Proc.new { raise ExpectedException } | ||
RaiseErrorMatcher.new(ExpectedException, nil).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" } | ||
RaiseErrorMatcher.new(ExpectedException, "message").matches?(proc).should == true | ||
end | ||
|
||
it "does not match when the proc does not raise the expected exception" do | ||
proc = Proc.new { raise UnexpectedException } | ||
RaiseErrorMatcher.new(ExpectedException, nil).matches?(proc).should == false | ||
end | ||
|
||
it "does not match when the proc raises the expected exception with an unexpected message" do | ||
proc = Proc.new { raise ExpectedException, "unexpected" } | ||
RaiseErrorMatcher.new(ExpectedException, "expected").matches?(proc).should == false | ||
end | ||
|
||
it "does not match when the proc does not raise an exception" do | ||
proc = Proc.new {} | ||
RaiseErrorMatcher.new(ExpectedException, "expected").matches?(proc).should == false | ||
end | ||
|
||
it "does not match when the raised exception is not an instance of the expected exception" do | ||
proc = Proc.new { raise Exception } | ||
RaiseErrorMatcher.new(ExpectedException, nil).matches?(proc).should == false | ||
end | ||
|
||
it "provides a useful failure message" do | ||
proc = Proc.new { raise UnexpectedException, "unexpected" } | ||
matcher = RaiseErrorMatcher.new(ExpectedException, "expected") | ||
matcher.matches?(proc) | ||
matcher.failure_message.should == | ||
["Expected ExpectedException (expected)", "but got UnexpectedException (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 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 |