Permalink
Browse files

When expecting an exception, capture it so the failure message can

access its data.
  • Loading branch information...
1 parent 771e2c9 commit 9c3884dc32cad13a9f4f6f2875e4107824613e16 @dchelimsky dchelimsky committed Jun 8, 2010
Showing with 22 additions and 10 deletions.
  1. +3 −3 lib/rspec/matchers/matcher.rb
  2. +1 −1 rspec-expectations.gemspec
  3. +18 −6 spec/rspec/matchers/matcher_spec.rb
@@ -5,13 +5,13 @@ class Matcher
include RSpec::Matchers::Pretty
include RSpec::Matchers
- attr_reader :expected, :actual
+ attr_reader :expected, :actual, :rescued_exception
def initialize(name, *expected, &declarations)
@name = name
@expected = expected
@actual = nil
@diffable = false
- @expected_exception = nil
+ @expected_exception, @rescued_exception = nil
@messages = {
:description => lambda {"#{name_to_sentence}#{expected_to_sentence}"},
:failure_message_for_should => lambda {|actual| "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"},
@@ -29,7 +29,7 @@ def matches?(actual)
begin
instance_exec(actual, &@match_block)
true
- rescue @expected_exception
+ rescue @expected_exception => @rescued_exception
false
end
else
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["David Chelimsky", "Chad Humphries"]
- s.date = %q{2010-06-06}
+ s.date = %q{2010-06-08}
s.description = %q{rspec expectations (should[_not] and matchers)}
s.email = %q{dchelimsky@gmail.com;chad.humphries@gmail.com}
s.extra_rdoc_files = [
@@ -230,11 +230,11 @@ def second_word
end
describe "#match_unless_raises" do
- context "with a passing assertion" do
+ context "with an assertion" do
let(:mod) do
Module.new do
def assert_equal(a,b)
- a == b ? nil : (raise UnexpectedError.new("#{a} does not equal #{b}"))
+ a == b ? nil : (raise UnexpectedError.new("#{b} does not equal #{a}"))
end
end
end
@@ -247,11 +247,23 @@ def assert_equal(a,b)
end
end
end
- it "passes as you would expect" do
- matcher.matches?(4).should be_true
+
+ context "with passing assertion" do
+ it "passes" do
+ matcher.matches?(4).should be_true
+ end
end
- it "fails as you would expect" do
- matcher.matches?(5).should be_false
+
+ context "with failing assertion" do
+ it "fails" do
+ matcher.matches?(5).should be_false
+ end
+
+ it "provides the raised exception" do
+ matcher.matches?(5)
+ matcher.rescued_exception.message.
+ should eq("5 does not equal 4")
+ end
end
end

0 comments on commit 9c3884d

Please sign in to comment.