Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false positive for RSpec/StubbedMock when stubbed message expectation with a block and block parameter #1546

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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