Module: Minitest::AssertChanges
- Included in:
- Test
- Defined in:
- lib/minitest/assert_changes.rb,
lib/minitest/assert_changes/version.rb
Constant Summary collapse
- UNTRACKED =
:nodoc:
Object.new
- VERSION =
"1.0.0"
Instance Method Summary collapse
-
#assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &block) ⇒ Object
Assertion that the result of evaluating an expression is changed before and after invoking the passed in block.
-
#assert_no_changes(expression, message = nil, &block) ⇒ Object
Assertion that the result of evaluating an expression is changed before and after invoking the passed in block.
Instance Method Details
#assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &block) ⇒ Object
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
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/minitest/assert_changes.rb', line 41 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 = yield unless from == UNTRACKED error = "#{expression.inspect} isn't #{from}" error = "#{}.\n#{error}" if assert from === before, error end after = exp.call if to == UNTRACKED error = "#{expression.inspect} didn't changed" error = "#{}.\n#{error}" if assert_not_equal before, after, error else = "#{expression.inspect} didn't change to #{to}" error = "#{}.\n#{error}" if assert to === after, error end retval end |
#assert_no_changes(expression, message = nil, &block) ⇒ Object
Assertion that the result of evaluating an expression is changed before and after invoking the passed in block.
assert_no_changes 'Status.all_good?' 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
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/minitest/assert_changes.rb', line 80 def assert_no_changes(expression, = nil, &block) exp = expression.respond_to?(:call) ? expression : -> { eval(expression.to_s, block.binding) } before = exp.call retval = yield after = exp.call error = "#{expression.inspect} did change to #{after}" error = "#{}.\n#{error}" if assert_equal before, after, error retval end |