Introduce assert_not to replace 'assert !foo'

This commit is contained in:
Jeremy Kemper 2012-12-28 10:34:26 -07:00
parent 6b18a79abe
commit f75addd06b
3 changed files with 39 additions and 0 deletions

View File

@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
* Introduce assert_not to replace warty 'assert !foo'. *Jeremy Kemper*
* Prevent `Callbacks#set_callback` from setting the same callback twice.
before_save :foo, :bar, :foo

View File

@ -3,6 +3,22 @@ require 'active_support/core_ext/object/blank'
module ActiveSupport
module Testing
module Assertions
# Assert that an expression is not truthy. Passes if <tt>object</tt> is
# +nil+ or +false+. "Truthy" means "considered true in a conditional"
# like <tt>if foo</tt>.
#
# assert_not nil # => true
# assert_not false # => true
# assert_not 'foo' # => 'foo' is not nil or false
#
# An error message can be specified.
#
# assert_not foo, 'foo should be false'
def assert_not(object, message = nil)
message ||= "Expected #{mu_pp(object)} to be nil or false"
assert !object, message
end
# Test numeric difference between the return value of an expression as a
# result of what is evaluated in the yielded block.
#

View File

@ -15,6 +15,27 @@ class AssertDifferenceTest < ActiveSupport::TestCase
@object.num = 0
end
def test_assert_not
assert_not nil
assert_not false
begin
assert_not true
rescue Exception => e
assert_equal 'Expected true to be nil or false', e.message
else
fail 'assert_not true should fail'
end
begin
assert_not true, 'custom'
rescue Exception => e
assert_equal 'custom', e.message
else
fail 'assert_not true should fail'
end
end
def test_assert_no_difference
assert_no_difference '@object.num' do
# ...