class RuboCop::Cop::RSpec::ExpectChange


expect(run).to change { user.reload.name }
expect(run).to change { Foo.bar(:count) }
# also good when there are arguments or chained method calls
expect(run).to change(foo, :baz)
expect(run).to change(Foo, :bar)
# good
expect(run).to change { foo.baz }
expect(run).to change { Foo.bar }
# bad
@example ‘EnforcedStyle: method_call`
expect(run).to change { Foo.bar }
# good
expect(run).to change(Foo, :bar)
# bad
@example `EnforcedStyle: block`
This cop can be configured using the `EnforcedStyle` option.
or passing a block that reads the attribute value.
Enforces either passing object and attribute as arguments to the matcher
Checks for consistent style of change matcher.

def autocorrect(node)

def autocorrect(node)
  if style == :block
    autocorrect_method_call_to_block(node)
  else
    autocorrect_block_to_method_call(node)
  end
end

def autocorrect_block_to_method_call(node)

def autocorrect_block_to_method_call(node)
  lambda do |corrector|
    expect_change_with_block(node) do |receiver, message|
      replacement = "change(#{receiver}, :#{message})"
      corrector.replace(node.loc.expression, replacement)
    end
  end
end

def autocorrect_method_call_to_block(node)

def autocorrect_method_call_to_block(node)
  lambda do |corrector|
    expect_change_with_arguments(node) do |receiver, message|
      replacement = "change { #{receiver}.#{message} }"
      corrector.replace(node.loc.expression, replacement)
    end
  end
end

def on_block(node)

def on_block(node)
  return unless style == :method_call
  expect_change_with_block(node) do |receiver, message|
    add_offense(
      node,
      message: format(MSG_BLOCK, obj: receiver, attr: message)
    )
  end
end

def on_send(node)

def on_send(node)
  return unless style == :block
  expect_change_with_arguments(node) do |receiver, message|
    add_offense(
      node,
      message: format(MSG_CALL, obj: receiver, attr: message)
    )
  end
end