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
43 44 45 46 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 43 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
62 63 64 65 66 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 62 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
130 131 132 133 134 135 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 130 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
82 83 84 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 82 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
109 110 111 112 113 114 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 109 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
94 95 96 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 94 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
35 36 37 38 39 40 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 35 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
137 138 139 140 141 |
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 137 def on_send(node) expectation(node) do |expectation, method_name, matcher| on_expectation(expectation, method_name, matcher) end end |