instance method assert_changes

Ruby on Rails 5.2.8.1

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_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

Parameters

expression req
message opt = nil
from key = UNTRACKED
to key = UNTRACKED
block block
Source
# File activesupport/lib/active_support/testing/assertions.rb, line 159
      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 = yield

        unless from == UNTRACKED
          error = "#{expression.inspect} isn't #{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
        assert before != after, error

        unless to == UNTRACKED
          error = "#{expression.inspect} didn't change to #{to}"
          error = "#{message}.\n#{error}" if message
          assert to === after, error
        end

        retval
      end

Defined in activesupport/lib/active_support/testing/assertions.rb line 159 · 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