Skip to content

Commit

Permalink
Fix a false positive for RSpec/StubbedMock when stubbed message exp…
Browse files Browse the repository at this point in the history
…ectation with a block and block parameter

Fix: #1518
  • Loading branch information
ydah committed Jan 11, 2023
1 parent a1ece2c commit 1b99171
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/stubbed_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rspec/stubbed_mock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 1b99171

Please sign in to comment.