Skip to content

Commit

Permalink
Added a :message option to assert_accepts/assert_rejects for testing …
Browse files Browse the repository at this point in the history
…failure messages
  • Loading branch information
jferris committed May 6, 2009
1 parent 673ffa6 commit 4d1497b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
24 changes: 18 additions & 6 deletions lib/shoulda/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,27 @@ def assert_does_not_contain(collection, x, extra_msg = "")
end

# Asserts that the given matcher returns true when +target+ is passed to #matches?
def assert_accepts(matcher, target)
success = matcher.matches?(target)
assert_block(matcher.failure_message) { success }
def assert_accepts(matcher, target, options = {})
if matcher.matches?(target)
assert_block { true }
if options[:message]
assert_match options[:message], matcher.negative_failure_message
end
else
assert_block(matcher.failure_message) { false }
end
end

# Asserts that the given matcher returns false when +target+ is passed to #matches?
def assert_rejects(matcher, target)
success = !matcher.matches?(target)
assert_block(matcher.negative_failure_message) { success }
def assert_rejects(matcher, target, options = {})
unless matcher.matches?(target)
assert_block { true }
if options[:message]
assert_match options[:message], matcher.failure_message
end
else
assert_block(matcher.negative_failure_message) { false }
end
end
end
end
24 changes: 22 additions & 2 deletions test/other/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,20 @@ class HelpersTest < Test::Unit::TestCase # :nodoc:
:negative_failure_message => 'big time failure')
end

should "pass when given to assert_accepts" do
should "pass when given to assert_accepts with no message expectation" do
assert_accepts @matcher, 'target'
end

should "pass when given to assert_accepts with a matching message" do
assert_accepts @matcher, 'target', :message => /big time/
end

should "fail when given to assert_accepts with non-matching message" do
assert_raise Test::Unit::AssertionFailedError do
assert_accepts @matcher, 'target', :message => /small time/
end
end

context "when given to assert_rejects" do
setup do
begin
Expand All @@ -217,10 +227,20 @@ class HelpersTest < Test::Unit::TestCase # :nodoc:
:negative_failure_message => 'bad failure message')
end

should "pass when given to assert_rejects" do
should "pass when given to assert_rejects with no message expectation" do
assert_rejects @matcher, 'target'
end

should "pass when given to assert_rejects with a matching message" do
assert_rejects @matcher, 'target', :message => /big time/
end

should "fail when given to assert_rejects with a non-matching message" do
assert_raise Test::Unit::AssertionFailedError do
assert_rejects @matcher, 'target', :message => /small time/
end
end

context "when given to assert_accepts" do
setup do
begin
Expand Down

0 comments on commit 4d1497b

Please sign in to comment.