2010-03-31 07:40:51 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2009-04-22 20:41:28 -04:00
|
|
|
|
2008-06-10 17:01:05 -04:00
|
|
|
module ActiveSupport
|
|
|
|
module Testing
|
|
|
|
module Assertions
|
2015-10-07 11:37:46 -04:00
|
|
|
# Asserts that an expression is not truthy. Passes if <tt>object</tt> is
|
2012-12-28 12:34:26 -05:00
|
|
|
# +nil+ or +false+. "Truthy" means "considered true in a conditional"
|
|
|
|
# like <tt>if foo</tt>.
|
|
|
|
#
|
|
|
|
# assert_not nil # => true
|
|
|
|
# assert_not false # => true
|
2014-06-12 13:16:39 -04:00
|
|
|
# assert_not 'foo' # => Expected "foo" to be nil or false
|
2012-12-28 12:34:26 -05:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2012-06-19 15:47:46 -04:00
|
|
|
# Test numeric difference between the return value of an expression as a
|
|
|
|
# result of what is evaluated in the yielded block.
|
2008-06-10 17:01:05 -04:00
|
|
|
#
|
|
|
|
# assert_difference 'Article.count' do
|
2015-05-23 19:34:15 -04:00
|
|
|
# post :create, params: { article: {...} }
|
2008-06-10 17:01:05 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# An arbitrary expression is passed in and evaluated.
|
|
|
|
#
|
2015-05-13 06:28:33 -04:00
|
|
|
# assert_difference 'Article.last.comments(:reload).size' do
|
2015-05-23 19:34:15 -04:00
|
|
|
# post :create, params: { comment: {...} }
|
2008-06-10 17:01:05 -04:00
|
|
|
# end
|
|
|
|
#
|
2012-06-19 15:47:46 -04:00
|
|
|
# An arbitrary positive or negative difference can be specified.
|
|
|
|
# The default is <tt>1</tt>.
|
2008-06-10 17:01:05 -04:00
|
|
|
#
|
|
|
|
# assert_difference 'Article.count', -1 do
|
2015-05-23 19:34:15 -04:00
|
|
|
# post :delete, params: { id: ... }
|
2008-06-10 17:01:05 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# An array of expressions can also be passed in and evaluated.
|
|
|
|
#
|
2012-06-19 15:47:46 -04:00
|
|
|
# assert_difference [ 'Article.count', 'Post.count' ], 2 do
|
2015-05-23 19:34:15 -04:00
|
|
|
# post :create, params: { article: {...} }
|
2008-06-10 17:01:05 -04:00
|
|
|
# end
|
|
|
|
#
|
2011-05-01 14:55:13 -04:00
|
|
|
# A lambda or a list of lambdas can be passed in and evaluated:
|
|
|
|
#
|
2012-09-14 23:57:38 -04:00
|
|
|
# assert_difference ->{ Article.count }, 2 do
|
2015-05-23 19:34:15 -04:00
|
|
|
# post :create, params: { article: {...} }
|
2011-05-01 14:55:13 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# assert_difference [->{ Article.count }, ->{ Post.count }], 2 do
|
2015-05-23 19:34:15 -04:00
|
|
|
# post :create, params: { article: {...} }
|
2011-05-01 14:55:13 -04:00
|
|
|
# end
|
|
|
|
#
|
2012-06-19 15:25:11 -04:00
|
|
|
# An error message can be specified.
|
2008-06-10 17:01:05 -04:00
|
|
|
#
|
2012-06-19 15:47:46 -04:00
|
|
|
# assert_difference 'Article.count', -1, 'An Article should be destroyed' do
|
2015-05-23 19:34:15 -04:00
|
|
|
# post :delete, params: { id: ... }
|
2008-06-10 17:01:05 -04:00
|
|
|
# end
|
2009-02-06 12:55:18 -05:00
|
|
|
def assert_difference(expression, difference = 1, message = nil, &block)
|
2012-01-05 23:03:53 -05:00
|
|
|
expressions = Array(expression)
|
2011-08-04 17:07:23 -04:00
|
|
|
|
|
|
|
exps = expressions.map { |e|
|
|
|
|
e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
|
2011-05-01 15:16:12 -04:00
|
|
|
}
|
2014-10-27 12:28:53 -04:00
|
|
|
before = exps.map(&:call)
|
2009-02-06 20:01:34 -05:00
|
|
|
|
2015-09-24 12:32:08 -04:00
|
|
|
retval = yield
|
2009-02-06 20:01:34 -05:00
|
|
|
|
2011-08-04 17:07:23 -04:00
|
|
|
expressions.zip(exps).each_with_index do |(code, e), i|
|
2011-08-03 19:55:00 -04:00
|
|
|
error = "#{code.inspect} didn't change by #{difference}"
|
2011-05-01 14:55:13 -04:00
|
|
|
error = "#{message}.\n#{error}" if message
|
2011-08-04 17:07:23 -04:00
|
|
|
assert_equal(before[i] + difference, e.call, error)
|
2008-06-10 17:01:05 -04:00
|
|
|
end
|
2015-09-24 12:32:08 -04:00
|
|
|
|
|
|
|
retval
|
2008-06-10 17:01:05 -04:00
|
|
|
end
|
|
|
|
|
2012-06-19 15:47:46 -04:00
|
|
|
# Assertion that the numeric result of evaluating an expression is not
|
|
|
|
# changed before and after invoking the passed in block.
|
2008-06-10 17:01:05 -04:00
|
|
|
#
|
|
|
|
# assert_no_difference 'Article.count' do
|
2015-05-23 19:34:15 -04:00
|
|
|
# post :create, params: { article: invalid_attributes }
|
2008-06-10 17:01:05 -04:00
|
|
|
# end
|
|
|
|
#
|
2012-06-19 15:25:11 -04:00
|
|
|
# An error message can be specified.
|
2008-06-10 17:01:05 -04:00
|
|
|
#
|
2012-06-19 15:47:46 -04:00
|
|
|
# assert_no_difference 'Article.count', 'An Article should not be created' do
|
2015-05-23 19:34:15 -04:00
|
|
|
# post :create, params: { article: invalid_attributes }
|
2008-06-10 17:01:05 -04:00
|
|
|
# end
|
2009-02-06 12:55:18 -05:00
|
|
|
def assert_no_difference(expression, message = nil, &block)
|
|
|
|
assert_difference expression, 0, message, &block
|
2008-06-10 17:01:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|