Method: ActiveSupport::Testing::Assertions#assert_no_changes

Defined in:
activesupport/lib/active_support/testing/assertions.rb

#assert_no_changes(expression, message = nil, from: UNTRACKED, &block) ⇒ Object

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


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'activesupport/lib/active_support/testing/assertions.rb', line 238

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}, got #{before.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