Skip to content
Browse files

match_unless_raises accepts multiple args

- in support of fix for rspec/rspec-rails#530
  • Loading branch information...
1 parent 429c672 commit a5a58aa7f8ee64ac13e1b023c3e4e72d1dff6844 @dchelimsky dchelimsky committed Apr 19, 2012
Showing with 13 additions and 6 deletions.
  1. +5 −4 lib/rspec/matchers/built_in/base_matcher.rb
  2. +8 −2 spec/rspec/matchers/base_matcher_spec.rb
View
9 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
View
10 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

0 comments on commit a5a58aa

Please sign in to comment.
Something went wrong with that request. Please try again.