diff --git a/lib/rspec/matchers/built_in/base_matcher.rb b/lib/rspec/matchers/built_in/base_matcher.rb index 24ed6e503..1cb441022 100644 --- a/lib/rspec/matchers/built_in/base_matcher.rb +++ b/lib/rspec/matchers/built_in/base_matcher.rb @@ -24,13 +24,14 @@ def matches?(actual) @actual = actual end - def match_unless_raises(exception=Exception) + def match_unless_raises(*exceptions) + exceptions.unshift Exception if exceptions.empty? begin yield - true - rescue exception => @rescued_exception - false + rescue *exceptions => @rescued_exception + return false end + true end def failure_message_for_should diff --git a/spec/rspec/matchers/base_matcher_spec.rb b/spec/rspec/matchers/base_matcher_spec.rb index 5a9e5ad1b..3759fb042 100644 --- a/spec/rspec/matchers/base_matcher_spec.rb +++ b/spec/rspec/matchers/base_matcher_spec.rb @@ -15,11 +15,17 @@ module RSpec::Matchers::BuiltIn matcher.match_unless_raises { raise }.should be_false end - it "returns false if the submitted error is raised" do + it "returns false if the only submitted error is raised" do matcher.match_unless_raises(RuntimeError){ raise "foo" }.should be_false end - it "re-raises any error other than the one specified" do + it "returns false if any of several errors submitted is raised" do + matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise "foo" }.should be_false + matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise ArgumentError.new('') }.should be_false + matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise NameError.new('') }.should be_false + end + + it "re-raises any error other than one of those specified" do expect do matcher.match_unless_raises(ArgumentError){ raise "foo" } end.to raise_error