Class: RuboCop::Cop::RSpec::ExpectChange
- Extended by:
- AutoCorrector
- Includes:
- ConfigurableEnforcedStyle
- Defined in:
- lib/rubocop/cop/rspec/expect_change.rb
Overview
Checks for consistent style of change matcher.
Enforces either passing a receiver and message as method arguments, or a block.
This cop can be configured using the ‘EnforcedStyle` option.
When using compound expectations with ‘change` and a negated matcher (e.g., `not_change`), you can configure the `NegatedMatcher` option to ensure consistent style enforcement across both matchers.
Constant Summary collapse
- MSG_BLOCK =
'Prefer `%<matcher>s(%<obj>s, :%<attr>s)`.'- MSG_CALL =
'Prefer `%<matcher>s { %<obj>s.%<attr>s }`.'
Instance Method Summary collapse
- #expect_matcher_with_arguments(node) ⇒ Object
- #expect_matcher_with_block(node) ⇒ Object
-
#on_block(node) ⇒ Object
rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler.
- #on_send(node) ⇒ Object
Methods inherited from Base
inherited, #on_new_investigation
Methods included from RSpec::Language
#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?
Instance Method Details
#expect_matcher_with_arguments(node) ⇒ Object
70 71 72 |
# File 'lib/rubocop/cop/rspec/expect_change.rb', line 70 def_node_matcher :expect_matcher_with_arguments, <<~PATTERN (send nil? _ $_ ({sym str} $_)) PATTERN |
#expect_matcher_with_block(node) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rubocop/cop/rspec/expect_change.rb', line 75 def_node_matcher :expect_matcher_with_block, <<~PATTERN (block (send nil? _) (args) (send ${ (send nil? _) # change { user.name } const # change { User.count } } $_ ) ) PATTERN |
#on_block(node) ⇒ Object
rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rubocop/cop/rspec/expect_change.rb', line 104 def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler return unless style == :method_call return unless matcher_method?(node.method_name) expect_matcher_with_block(node) do |receiver, | matcher_name = node.method_name.to_s msg = format(MSG_BLOCK, matcher: matcher_name, obj: receiver.source, attr: ) add_offense(node, message: msg) do |corrector| replacement = "#{matcher_name}(#{receiver.source}, :#{})" corrector.replace(node, replacement) end end end |
#on_send(node) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rubocop/cop/rspec/expect_change.rb', line 89 def on_send(node) return unless style == :block return unless matcher_method?(node.method_name) expect_matcher_with_arguments(node) do |receiver, | matcher_name = node.method_name.to_s msg = format(MSG_CALL, matcher: matcher_name, obj: receiver.source, attr: ) add_offense(node, message: msg) do |corrector| replacement = "#{matcher_name} { #{receiver.source}.#{} }" corrector.replace(node, replacement) end end end |