Skip to content
This repository
Browse code

add match_unless_raises to BaseMatcher

  • Loading branch information...
commit 94591f284b7efb5658d1ddff7ba913c6328e3475 1 parent e61595a
David Chelimsky dchelimsky authored
9 lib/rspec/matchers/base_matcher.rb
@@ -21,6 +21,15 @@ def matches?(actual)
21 21 @actual = actual
22 22 end
23 23
  24 + def match_unless_raises(exception=Exception)
  25 + begin
  26 + yield
  27 + true
  28 + rescue exception => e
  29 + false
  30 + end
  31 + end
  32 +
24 33 def failure_message_for_should
25 34 "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"
26 35 end
31 spec/rspec/matchers/base_matcher_spec.rb
... ... @@ -0,0 +1,31 @@
  1 +module RSpec::Matchers
  2 +
  3 + describe BaseMatcher do
  4 + describe "#match_unless_raises" do
  5 + let(:matcher) do
  6 + Class.new do
  7 + include BaseMatcher
  8 + end.new
  9 + end
  10 +
  11 + it "returns true if there are no errors" do
  12 + matcher.match_unless_raises {}.should be_true
  13 + end
  14 +
  15 + it "returns false if there is an error" do
  16 + matcher.match_unless_raises { raise }.should be_false
  17 + end
  18 +
  19 + it "returns false if the submitted error is raised" do
  20 + matcher.match_unless_raises(RuntimeError){ raise "foo" }.should be_false
  21 + end
  22 +
  23 + it "re-raises any error other than the one specified" do
  24 + expect do
  25 + matcher.match_unless_raises(ArgumentError){ raise "foo" }
  26 + end.to raise_error
  27 + end
  28 + end
  29 + end
  30 +
  31 +end

0 comments on commit 94591f2

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