Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Introduce assert_not to replace 'assert !foo'

  • Loading branch information...
commit f75addd06bbdde925d095a4d27ddd6fbdd9336ba 1 parent 6b18a79
@jeremy jeremy authored
View
2  activesupport/CHANGELOG.md
@@ -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
16 activesupport/lib/active_support/testing/assertions.rb
@@ -3,6 +3,22 @@
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
21 activesupport/test/test_test.rb
@@ -15,6 +15,27 @@ def decrement
@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
@tenderlove Owner
e = assert_raises(Exception) { assert_not true }
assert_equal 'Expected true to be nil or false', e.message
@jeremy Owner
jeremy added a note

Derp! 8a130ec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ 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
# ...

6 comments on commit f75addd

@splattael

Why reimplementing minitest's refute?

@jeremy
Owner

@splattael We retain the traditional assert_* API for aesthetics and backward compatibility. This fills a niche there. (Didn't alias the method since it uses a different message and return value.)

@splattael

@jeremy Thanks for the explanation.
What do you mean by "backward compatibility"? I thought Rails master is >= 1.9.3?

@tenderlove
Owner

@jeremy the message can be customized by calling super with the message:

def refute obj, message = nil
  message ||= "Expected #{mu_pp(object)} to be nil or false"
  !super(obj, message)
end
alias :assert_not :refute

(edited to take into account the return value)

@jeremy
Owner

@tenderlove Thinking about it. That reuses #refute, but alters its return value. And RDoc for assert_not shows up as an alias instead of an original method.

@jeremy
Owner

@tenderlove Better to have assert_not invoke refute rather than change refute and alias assert_not, I think. But then the code looks like

        !refute(object, message)

vs

        assert !object, message

Think the second one — an assertion that the obj isn't truthy — is clearer than a not-refutation that the object is truthy.

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