module ActiveSupport::Testing::Assertions
def _assert_nothing_raised_or_warn(assertion, &block)
def _assert_nothing_raised_or_warn(assertion, &block) assert_nothing_raised(&block) rescue Minitest::UnexpectedError => e if tagged_logger && tagged_logger.warn? warning = <<~MSG #{self.class} - #{name}: #{e.error.class} raised. If you expected this exception, use `assert_raises` as near to the code that raises as possible. Other block based assertions (e.g. `#{assertion}`) can be used, as long as `assert_raises` is inside their block. MSG tagged_logger.warn warning end raise end
def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &block)
post :create, params: { status: { incident: true } }
assert_changes -> { Status.all_good? }, 'Expected the status to be bad' do
An error message can be specified.
end
@object = :foo
assert_changes :@object, from: nil, to: :foo do
executed.
expected initial value and the expected value after the block was
The keyword arguments +:from+ and +:to+ can be given to specify the
end
@object = 42
assert_changes :@object do
anything that can be converted to string with #to_s.
The assertion is useful to test side effects. The passed block can be
end
post :create, params: { status: { ok: false } }
assert_changes -> { Status.all_good? } do
the block. A lambda can be passed for the block as well.
You can pass the block as a string to be evaluated in the context of
end
post :create, params: { status: { ok: false } }
assert_changes 'Status.all_good?' do
and after invoking the passed in block.
Assertion that the result of evaluating an expression is changed before
def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &block) exp = expression.respond_to?(:call) ? expression : -> { eval(expression.to_s, block.binding) } before = exp.call retval = _assert_nothing_raised_or_warn("assert_changes", &block) unless from == UNTRACKED error = "Expected change from #{from.inspect}" error = "#{message}.\n#{error}" if message assert from === before, error end after = exp.call error = "#{expression.inspect} didn't change" error = "#{error}. It was already #{to}" if before == to error = "#{message}.\n#{error}" if message refute_equal before, after, error unless to == UNTRACKED error = "Expected change to #{to}\n" error = "#{message}.\n#{error}" if message assert to === after, error end retval end
def assert_difference(expression, *args, &block)
post :delete, params: { id: ... }
assert_difference 'Article.count', -1, 'An Article should be destroyed' do
An error message can be specified.
end
post :create, params: { article: {...} }
assert_difference [->{ Article.count }, ->{ Post.count }], 2 do
end
post :create, params: { article: {...} }
assert_difference ->{ Article.count }, 2 do
A lambda or a list of lambdas can be passed in and evaluated:
end
post :create, params: { article: {...} }
assert_difference ->{ Article.count } => 1, ->{ Notification.count } => 2 do
A hash of expressions/numeric differences can also be passed in and evaluated.
end
post :create, params: { article: {...} }
assert_difference [ 'Article.count', 'Post.count' ], 2 do
An array of expressions can also be passed in and evaluated.
end
post :delete, params: { id: ... }
assert_difference 'Article.count', -1 do
The default is 1.
An arbitrary positive or negative difference can be specified.
end
post :create, params: { comment: {...} }
assert_difference 'Article.last.comments(:reload).size' do
An arbitrary expression is passed in and evaluated.
end
post :create, params: { article: {...} }
assert_difference 'Article.count' do
result of what is evaluated in the yielded block.
Test numeric difference between the return value of an expression as a
def assert_difference(expression, *args, &block) expressions = if expression.is_a?(Hash) message = args[0] expression else difference = args[0] || 1 message = args[1] Array(expression).index_with(difference) end exps = expressions.keys.map { |e| e.respond_to?(:call) ? e : lambda { eval(e, block.binding) } } before = exps.map(&:call) retval = _assert_nothing_raised_or_warn("assert_difference", &block) expressions.zip(exps, before) do |(code, diff), exp, before_value| error = "#{code.inspect} didn't change by #{diff}" error = "#{message}.\n#{error}" if message assert_equal(before_value + diff, exp.call, error) end retval end
def assert_no_changes(expression, message = nil, from: UNTRACKED, &block)
post :create, params: { status: { ok: false } }
assert_no_changes -> { Status.all_good? }, 'Expected the status to be good' do
An error message can be specified.
end
post :create, params: { status: { ok: true } }
assert_no_changes -> { Status.all_good? }, from: true do
initial value.
Provide the optional keyword argument :from to specify the expected
end
post :create, params: { status: { ok: true } }
assert_no_changes 'Status.all_good?' do
and after invoking the passed in block.
Assertion that the result of evaluating an expression is not changed before
def assert_no_changes(expression, message = nil, from: UNTRACKED, &block) exp = expression.respond_to?(:call) ? expression : -> { eval(expression.to_s, block.binding) } before = exp.call retval = _assert_nothing_raised_or_warn("assert_no_changes", &block) unless from == UNTRACKED error = "Expected initial value of #{from.inspect}" error = "#{message}.\n#{error}" if message assert from === before, error end after = exp.call error = "#{expression.inspect} changed" error = "#{message}.\n#{error}" if message if before.nil? assert_nil after, error else assert_equal before, after, error end retval end
def assert_no_difference(expression, message = nil, &block)
post :create, params: { article: invalid_attributes }
assert_no_difference [ 'Article.count', -> { Post.count } ] do
An array of expressions can also be passed in and evaluated.
end
post :create, params: { article: invalid_attributes }
assert_no_difference 'Article.count', 'An Article should not be created' do
An error message can be specified.
end
post :create, params: { article: invalid_attributes }
assert_no_difference -> { Article.count } do
A lambda can be passed in and evaluated.
end
post :create, params: { article: invalid_attributes }
assert_no_difference 'Article.count' do
changed before and after invoking the passed in block.
Assertion that the numeric result of evaluating an expression is not
def assert_no_difference(expression, message = nil, &block) assert_difference expression, 0, message, &block end
def assert_not(object, message = nil)
An error message can be specified.
assert_not 'foo' # => Expected "foo" to be nil or false
assert_not false # => true
assert_not nil # => true
like if foo.
+nil+ or +false+. "Truthy" means "considered true in a conditional"
Asserts that an expression is not truthy. Passes if object is
def assert_not(object, message = nil) message ||= "Expected #{mu_pp(object)} to be nil or false" assert !object, message end
def assert_nothing_raised
perform_service(param: 'no_exception')
assert_nothing_raised do
Passes if evaluated code in the yielded block raises no exception.
Assertion that the block should not raise an exception.
def assert_nothing_raised yield rescue => error raise Minitest::UnexpectedError.new(error) end