lib/rubocop/cop/rspec/expect_change.rb in rubocop-rspec-1.41.0 vs lib/rubocop/cop/rspec/expect_change.rb in rubocop-rspec-1.42.0

- old
+ new

@@ -28,10 +28,11 @@ # # also good when there are arguments or chained method calls # expect { run }.to change { Foo.bar(:count) } # expect { run }.to change { user.reload.name } # class ExpectChange < Cop + extend AutoCorrector include ConfigurableEnforcedStyle MSG_BLOCK = 'Prefer `change(%<obj>s, :%<attr>s)`.' MSG_CALL = 'Prefer `change { %<obj>s.%<attr>s }`.' @@ -49,51 +50,25 @@ 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) - ) + msg = format(MSG_CALL, obj: receiver, attr: message) + add_offense(node, message: msg) do |corrector| + replacement = "change { #{receiver}.#{message} }" + corrector.replace(node, replacement) + end end end 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 autocorrect(node) - if style == :block - autocorrect_method_call_to_block(node) - else - autocorrect_block_to_method_call(node) - end - end - - private - - 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 autocorrect_block_to_method_call(node) - lambda do |corrector| - expect_change_with_block(node) do |receiver, message| + msg = format(MSG_BLOCK, obj: receiver, attr: message) + add_offense(node, message: msg) do |corrector| replacement = "change(#{receiver}, :#{message})" - corrector.replace(node.loc.expression, replacement) + corrector.replace(node, replacement) end end end end end