From 1b99171a11f7e805748e840c4c12005cf0bc75a1 Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Wed, 11 Jan 2023 19:01:08 +0900 Subject: [PATCH] Fix a false positive for `RSpec/StubbedMock` when stubbed message expectation with a block and block parameter Fix: https://github.com/rubocop/rubocop-rspec/issues/1518 --- CHANGELOG.md | 1 + lib/rubocop/cop/rspec/stubbed_mock.rb | 2 +- spec/rubocop/cop/rspec/stubbed_mock_spec.rb | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49a0441cb..b2988a595 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Add new `RSpec/Rails/MinitestAssertions` cop. ([@ydah]) - Fix a false positive for `RSpec/PendingWithoutReason` when not inside example. ([@ydah]) - Fix a false negative for `RSpec/PredicateMatcher` when using `include` and `respond_to`. ([@ydah]) +- Fix a false positive for `RSpec/StubbedMock` when stubbed message expectation with a block and block parameter. ([@ydah]) ## 2.16.0 (2022-12-13) diff --git a/lib/rubocop/cop/rspec/stubbed_mock.rb b/lib/rubocop/cop/rspec/stubbed_mock.rb index fc1f413d1..3db637b9c 100644 --- a/lib/rubocop/cop/rspec/stubbed_mock.rb +++ b/lib/rubocop/cop/rspec/stubbed_mock.rb @@ -91,7 +91,7 @@ class StubbedMock < Base # @param node [RuboCop::AST::Node] # @yield [RuboCop::AST::Node] matcher def_node_matcher :matcher_with_return_block, <<~PATTERN - (block #message_expectation? args _) # receive(:foo) { 'bar' } + (block #message_expectation? (args) _) # receive(:foo) { 'bar' } PATTERN # @!method matcher_with_hash(node) diff --git a/spec/rubocop/cop/rspec/stubbed_mock_spec.rb b/spec/rubocop/cop/rspec/stubbed_mock_spec.rb index bd30d2735..aa8cabd33 100644 --- a/spec/rubocop/cop/rspec/stubbed_mock_spec.rb +++ b/spec/rubocop/cop/rspec/stubbed_mock_spec.rb @@ -15,6 +15,12 @@ RUBY end + it 'ignores stubbed message expectation with a block and block parameter' do + expect_no_offenses(<<-RUBY) + expect(foo).to receive(:bar) { |x| bar } + RUBY + end + it 'flags stubbed message expectation with argument matching' do expect_offense(<<-RUBY) expect(foo).to receive(:bar).with(42).and_return('hello world')