instance method assert_no_changes

Ruby on Rails 8.0.4

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

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

Parameters

expression req
message opt = nil
from key = UNTRACKED
block block
Source
# File activesupport/lib/active_support/testing/assertions.rb, line 252
      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
          rich_message = -> do
            error = "Expected initial value of #{from.inspect}, got #{before.inspect}"
            error = "#{message}.\n#{error}" if message
            error
          end
          assert from === before, rich_message
        end

        after = exp.call

        rich_message = -> do
          code_string = expression.respond_to?(:call) ? _callable_to_source_string(expression) : expression
          error = "`#{code_string}` changed"
          error = "#{message}.\n#{error}" if message
          error
        end

        if before.nil?
          assert_nil after, rich_message
        else
          assert_equal before, after, rich_message
        end

        retval
      end

Defined in activesupport/lib/active_support/testing/assertions.rb line 252 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::Testing::Assertions

Type at least 2 characters to search.

↑↓ navigate · open · esc close