Skip to content

Commit

Permalink
match_unless_raises accepts multiple args
Browse files Browse the repository at this point in the history
- in support of fix for rspec/rspec-rails#530
  • Loading branch information
dchelimsky committed Apr 20, 2012
1 parent 429c672 commit a5a58aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
9 changes: 5 additions & 4 deletions lib/rspec/matchers/built_in/base_matcher.rb
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions spec/rspec/matchers/base_matcher_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit a5a58aa

Please sign in to comment.