Module: ActiveSupport::Testing::Assertions
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
::ActionCable::Channel::TestCase ,
::ActionCable::Connection::TestCase ,
::ActionCable::TestCase ,
::ActionController::TestCase ,
::ActionDispatch::IntegrationTest ,
::ActionDispatch::SystemTestCase ,
::ActionMailbox::TestCase ,
::ActionMailer::TestCase ,
::ActionMailer::TestCase::Behavior ,
::ActionMailer::TestHelper ,
::ActionView::TestCase ,
::ActiveJob::TestCase ,
::ActiveJob::TestHelper ,
::ActiveSupport::TestCase ,
::Rails::Generators::TestCase
| |
Defined in: | activesupport/lib/active_support/testing/assertions.rb |
Constant Summary
-
UNTRACKED =
Internal use only
# File 'activesupport/lib/active_support/testing/assertions.rb', line 8Object.new
Instance Method Summary
-
#assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &block)
Assertion that the result of evaluating an expression is changed before and after invoking the passed in block.
-
#assert_difference(expression, *args, &block)
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.
-
#assert_no_changes(expression, message = nil, from: UNTRACKED, &block)
Assertion that the result of evaluating an expression is not changed before and after invoking the passed in block.
-
#assert_no_difference(expression, message = nil, &block)
Assertion that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.
-
#assert_not(object, message = nil)
Asserts that an expression is not truthy.
-
#assert_nothing_raised
Assertion that the block should not raise an exception.
-
#assert_raise(*exp, match: nil, &block)
Alias for #assert_raises.
-
#assert_raises(*exp, match: nil, &block)
(also: #assert_raise)
Asserts that a block raises one of
exp
. - #_assert_nothing_raised_or_warn(assertion, &block) private
- #_callable_to_source_string(callable) private
Instance Method Details
#_assert_nothing_raised_or_warn(assertion, &block) (private)
[ GitHub ]# File 'activesupport/lib/active_support/testing/assertions.rb', line 286
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
#_callable_to_source_string(callable) (private)
[ GitHub ]# File 'activesupport/lib/active_support/testing/assertions.rb', line 301
def _callable_to_source_string(callable) if defined?(RubyVM::InstructionSequence) && callable.is_a?(Proc) iseq = RubyVM::InstructionSequence.of(callable) source = if iseq.script_lines iseq.script_lines.join("\n") elsif File.readable?(iseq.absolute_path) File.read(iseq.absolute_path) end return callable unless source location = iseq.to_a[4][:code_location] return callable unless location lines = source.lines[(location[0] - 1)..(location[2] - 1)] lines[-1] = lines[-1].byteslice(...location[3]) lines[0] = lines[0].byteslice(location[1]...) source = lines.join.strip # We ignore procs defined with do/end as they are likely multi-line anyway. if source.start_with?("{") source.delete_suffix!("}") source.delete_prefix!("{") source.strip! # It won't read nice if the callable contains multiple # lines, and it should be a rare occurrence anyway. # Same if it takes arguments. if !source.include?("\n") && !source.start_with?("|") return source end end end callable end
#assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &block)
Assertion that the result of evaluating an expression is changed before and after invoking the passed in block.
assert_changes 'Status.all_good?' do
post :create, params: { status: { ok: false } }
end
You can pass the block as a string to be evaluated in the context of the block. A lambda can be passed for the block as well.
assert_changes -> { Status.all_good? } do
post :create, params: { status: { ok: false } }
end
The assertion is useful to test side effects. The passed block can be anything that can be converted to string with #to_s
.
assert_changes :@object do
@object = 42
end
The keyword arguments :from
and :to
can be given to specify the expected initial value and the expected value after the block was executed.
assert_changes :@object, from: nil, to: :foo do
@object = :foo
end
An error message can be specified.
assert_changes -> { Status.all_good? }, 'Expected the status to be bad' do
post :create, params: { status: { incident: true } }
end
# File 'activesupport/lib/active_support/testing/assertions.rb', line 195
def assert_changes(expression, = 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 = -> do error = "Expected change from #{from.inspect}, got #{before.inspect}" error = "#{}.\n#{error}" if error end assert from === before, end after = exp.call = -> do code_string = expression.respond_to?(:call) ? _callable_to_source_string(expression) : expression error = "`#{code_string}` didn't change" error = "#{error}. It was already #{to.inspect}" if before == to error = "#{}.\n#{error}" if error end refute_equal before, after, unless to == UNTRACKED = -> do error = "Expected change to #{to.inspect}, got #{after.inspect}\n" error = "#{}.\n#{error}" if error end assert to === after, end retval end
#assert_difference(expression, *args, &block)
Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.
assert_difference 'Article.count' do
post :create, params: { article: {...} }
end
An arbitrary expression is passed in and evaluated.
assert_difference 'Article.last.comments(:reload).size' do
post :create, params: { comment: {...} }
end
An arbitrary positive or negative difference can be specified. The default is 1
.
assert_difference 'Article.count', -1 do
post :delete, params: { id: ... }
end
An array of expressions can also be passed in and evaluated.
assert_difference [ 'Article.count', 'Post.count' ], 2 do
post :create, params: { article: {...} }
end
A hash of expressions/numeric differences can also be passed in and evaluated.
assert_difference ->{ Article.count } => 1, ->{ Notification.count } => 2 do
post :create, params: { article: {...} }
end
A lambda or a list of lambdas can be passed in and evaluated:
assert_difference ->{ Article.count }, 2 do
post :create, params: { article: {...} }
end
assert_difference [->{ Article.count }, ->{ Post.count }], 2 do
post :create, params: { article: {...} }
end
An error message can be specified.
assert_difference 'Article.count', -1, 'An Article should be destroyed' do
post :delete, params: { id: ... }
end
# File 'activesupport/lib/active_support/testing/assertions.rb', line 101
def assert_difference(expression, *args, &block) expressions = if expression.is_a?(Hash) = args[0] expression else difference = args[0] || 1 = 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| actual = exp.call = -> do code_string = code.respond_to?(:call) ? _callable_to_source_string(code) : code error = "`#{code_string}` didn't change by #{diff}, but by #{actual - before_value}" error = "#{}.\n#{error}" if error end assert_equal(before_value + diff, actual, ) end retval end
#assert_no_changes(expression, message = nil, from: UNTRACKED, &block)
Assertion that the result of evaluating an expression is not changed before and after invoking the passed in block.
assert_no_changes 'Status.all_good?' do
post :create, params: { status: { ok: true } }
end
Provide the optional keyword argument :from
to specify the expected initial value.
assert_no_changes -> { Status.all_good? }, from: true do
post :create, params: { status: { ok: true } }
end
An error message can be specified.
assert_no_changes -> { Status.all_good? }, 'Expected the status to be good' do
post :create, params: { status: { ok: false } }
end
# File 'activesupport/lib/active_support/testing/assertions.rb', line 252
def assert_no_changes(expression, = 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 = -> do error = "Expected initial value of #{from.inspect}, got #{before.inspect}" error = "#{}.\n#{error}" if error end assert from === before, end after = exp.call = -> do code_string = expression.respond_to?(:call) ? _callable_to_source_string(expression) : expression error = "`#{code_string}` changed" error = "#{}.\n#{error}" if error end if before.nil? assert_nil after, else assert_equal before, after, end retval end
#assert_no_difference(expression, message = nil, &block)
Assertion that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.
assert_no_difference 'Article.count' do
post :create, params: { article: invalid_attributes }
end
A lambda can be passed in and evaluated.
assert_no_difference -> { Article.count } do
post :create, params: { article: invalid_attributes }
end
An error message can be specified.
assert_no_difference 'Article.count', 'An Article should not be created' do
post :create, params: { article: invalid_attributes }
end
An array of expressions can also be passed in and evaluated.
assert_no_difference [ 'Article.count', -> { Post.count } ] do
post :create, params: { article: invalid_attributes }
end
# File 'activesupport/lib/active_support/testing/assertions.rb', line 157
def assert_no_difference(expression, = nil, &block) assert_difference expression, 0, , &block end
#assert_not(object, message = nil)
Asserts that an expression is not truthy. Passes if object
is nil
or false
. “Truthy” means “considered true in a conditional” like if foo
.
assert_not nil # => true
assert_not false # => true
assert_not 'foo' # => Expected "foo" to be nil or false
An error message can be specified.
assert_not foo, 'foo should be false'
# File 'activesupport/lib/active_support/testing/assertions.rb', line 21
def assert_not(object, = nil) ||= -> { "Expected #{mu_pp(object)} to be nil or false" } assert !object, end
#assert_nothing_raised
Assertion that the block should not raise an exception.
Passes if evaluated code in the yielded block raises no exception.
assert_nothing_raised do
perform_service(param: 'no_exception')
end
# File 'activesupport/lib/active_support/testing/assertions.rb', line 48
def assert_nothing_raised yield.tap { assert(true) } rescue => error raise Minitest::UnexpectedError.new(error) end
#assert_raise(*exp, match: nil, &block)
Alias for #assert_raises.
# File 'activesupport/lib/active_support/testing/assertions.rb', line 39
alias :assert_raise :assert_raises
#assert_raises(*exp, match: nil, &block) Also known as: #assert_raise
Asserts that a block raises one of exp
. This is an enhancement of the standard ::Minitest
assertion method with the ability to test error messages.
assert_raises(ArgumentError, match: /incorrect param/i) do
perform_service(param: 'exception')
end
# File 'activesupport/lib/active_support/testing/assertions.rb', line 34
def assert_raises(*exp, match: nil, &block) error = super(*exp, &block) assert_match(match, error. ) if match error end