Permalink
Browse files

add match_unless_raises to BaseMatcher

  • Loading branch information...
1 parent e61595a commit 94591f284b7efb5658d1ddff7ba913c6328e3475 @dchelimsky dchelimsky committed Nov 24, 2011
Showing with 40 additions and 0 deletions.
  1. +9 −0 lib/rspec/matchers/base_matcher.rb
  2. +31 −0 spec/rspec/matchers/base_matcher_spec.rb
View
9 lib/rspec/matchers/base_matcher.rb
@@ -21,6 +21,15 @@ def matches?(actual)
@actual = actual
end
+ def match_unless_raises(exception=Exception)
+ begin
+ yield
+ true
+ rescue exception => e
+ false
+ end
+ end
+
def failure_message_for_should
"expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"
end
View
31 spec/rspec/matchers/base_matcher_spec.rb
@@ -0,0 +1,31 @@
+module RSpec::Matchers
+
+ describe BaseMatcher do
+ describe "#match_unless_raises" do
+ let(:matcher) do
+ Class.new do
+ include BaseMatcher
+ end.new
+ end
+
+ it "returns true if there are no errors" do
+ matcher.match_unless_raises {}.should be_true
+ end
+
+ it "returns false if there is an error" do
+ matcher.match_unless_raises { raise }.should be_false
+ end
+
+ it "returns false if the 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
+ expect do
+ matcher.match_unless_raises(ArgumentError){ raise "foo" }
+ end.to raise_error
+ end
+ end
+ end
+
+end

0 comments on commit 94591f2

Please sign in to comment.