Class: RuboCop::Cop::RSpec::StubbedMock
- Defined in:
- lib/rubocop/cop/rspec/stubbed_mock.rb
Overview
Checks that message expectations do not have a configured response.
Constant Summary collapse
- MSG =
'Prefer `%<replacement>s` over `%<method_name>s` when ' \ 'configuring a response.'
- RESTRICT_ON_SEND =
%i[to].freeze
Instance Method Summary collapse
- #configured_response?(node) ⇒ Object
-
#expectation(node) {|RuboCop::AST::Node| ... } ⇒ Object
Match expectation.
-
#matcher_with_blockpass(node) {|RuboCop::AST::Node| ... } ⇒ Object
Match matcher with a configured response in block-pass.
-
#matcher_with_configured_response(node) {|RuboCop::AST::Node| ... } ⇒ Object
Match matcher with a configured response.
-
#matcher_with_hash(node) {|RuboCop::AST::Node| ... } ⇒ Object
Match matcher with a configured response defined as a hash.
-
#matcher_with_return_block(node) {|RuboCop::AST::Node| ... } ⇒ Object
Match matcher with a return block.
-
#message_expectation?(node) ⇒ Array<RuboCop::AST::Node>
Match message expectation matcher.
- #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
#configured_response?(node) ⇒ Object
42 43 44 45 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 42 def_node_matcher :configured_response?, <<~PATTERN { :and_return :and_raise :and_throw :and_yield :and_call_original :and_wrap_original } PATTERN |
#expectation(node) {|RuboCop::AST::Node| ... } ⇒ Object
Match expectation
61 62 63 64 65 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 61 def_node_matcher :expectation, <<~PATTERN (send $(send nil? $#Expectations.all ...) :to $_) PATTERN |
#matcher_with_blockpass(node) {|RuboCop::AST::Node| ... } ⇒ Object
Match matcher with a configured response in block-pass
129 130 131 132 133 134 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 129 def_node_matcher :matcher_with_blockpass, <<~PATTERN { (send nil? { :receive :receive_message_chain } ... block_pass) # receive(:foo, &canned) (send (send nil? :receive ...) :with ... block_pass) # receive(:foo).with('foo', &canned) } PATTERN |
#matcher_with_configured_response(node) {|RuboCop::AST::Node| ... } ⇒ Object
Match matcher with a configured response
81 82 83 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 81 def_node_matcher :matcher_with_configured_response, <<~PATTERN (send #message_expectation? #configured_response? _) PATTERN |
#matcher_with_hash(node) {|RuboCop::AST::Node| ... } ⇒ Object
Match matcher with a configured response defined as a hash
108 109 110 111 112 113 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 108 def_node_matcher :matcher_with_hash, <<~PATTERN { (send nil? :receive_messages hash) # receive_messages(foo: 'bar', baz: 'qux') (send nil? :receive_message_chain ... hash) # receive_message_chain(:foo, bar: 'baz') } PATTERN |
#matcher_with_return_block(node) {|RuboCop::AST::Node| ... } ⇒ Object
Match matcher with a return block
93 94 95 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 93 def_node_matcher :matcher_with_return_block, <<~PATTERN (block #message_expectation? (args) _) # receive(:foo) { 'bar' } PATTERN |
#message_expectation?(node) ⇒ Array<RuboCop::AST::Node>
Match message expectation matcher
34 35 36 37 38 39 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 34 def_node_matcher :message_expectation?, <<~PATTERN { (send nil? { :receive :receive_message_chain } ...) # receive(:foo) (send (send nil? :receive ...) :with ...) # receive(:foo).with('bar') } PATTERN |
#on_send(node) ⇒ Object
138 139 140 141 142 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 138 def on_send(node) expectation(node) do |expectation, method_name, matcher| on_expectation(expectation, method_name, matcher) end end |