Skip to content

Commit

Permalink
Merge pull request #1690 from marocchino/fix-receive-messages
Browse files Browse the repository at this point in the history
Fix a few bugs in RSpec/ReceiveMessages
  • Loading branch information
ydah authored Aug 9, 2023
2 parents 03df044 + c86a27d commit 217a8dc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Master (Unreleased)

- Fix an incorrect autocorrect for `RSpec/ReceiveMessages` when method is only non-word character. ([@marocchino])
- Fix a false positive for `RSpec/ReceiveMessages` when return with splat. ([@marocchino])

## 2.23.1 (2023-08-07)

- Mark to `Safe: false` for `RSpec/Rails/NegationBeValid` cop. ([@ydah])
Expand Down Expand Up @@ -843,6 +846,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@lokhi]: https://github.com/lokhi
[@luke-hill]: https://github.com/luke-hill
[@m-yamashita01]: https://github.com/M-Yamashita01
[@marocchino]: https://github.com/marocchino
[@miguelfteixeira]: https://github.com/miguelfteixeira
[@mkenyon]: https://github.com/mkenyon
[@mkrawc]: https://github.com/mkrawc
Expand Down
9 changes: 5 additions & 4 deletions lib/rubocop/cop/rspec/receive_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ReceiveMessages < Base

# @!method allow_receive_message?(node)
def_node_matcher :allow_receive_message?, <<~PATTERN
(send (send nil? :allow ...) :to (send (send nil? :receive (sym _)) :and_return !#heredoc?))
(send (send nil? :allow ...) :to (send (send nil? :receive (sym _)) :and_return !#heredoc_or_splat?))
PATTERN

# @!method allow_argument(node)
Expand Down Expand Up @@ -147,12 +147,13 @@ def item_range_by_whole_lines(item)
range_by_whole_lines(item.source_range, include_final_newline: true)
end

def heredoc?(node)
(node.str_type? || node.dstr_type?) && node.heredoc?
def heredoc_or_splat?(node)
(node.str_type? || node.dstr_type?) && node.heredoc? ||
node.splat_type?
end

def requires_quotes?(value)
value.match?(/^:".*?"|=$/)
value.match?(/^:".*?"|=$|^\W+$/)
end
end
end
Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/cop/rspec/receive_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@
RUBY
end

it 'registers an offense when multiple messeages stubbed on the ' \
'same object and symbol methods' do
expect_offense(<<~RUBY)
before do
allow(Service).to receive(:`).and_return(true)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `receive_messages` instead of multiple stubs on lines [3].
allow(Service).to receive(:[]).and_return(true)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `receive_messages` instead of multiple stubs on lines [2].
end
RUBY

expect_correction(<<~RUBY)
before do
allow(Service).to receive_messages('`': true, '[]': true)
end
RUBY
end

it 'registers an offense when multiple messeages stubbed on the ' \
'same object and return array' do
expect_offense(<<~RUBY)
Expand Down Expand Up @@ -100,6 +118,26 @@
RUBY
end

it 'does not register an offense when multiple messeages stubbed on the ' \
'same object and return with splat' do
expect_no_offenses(<<~RUBY)
before do
allow(Service).to receive(:foo).and_return(*array)
allow(Service).to receive(:bar).and_return(*array)
end
RUBY
end

it 'does not register an offense when multiple messeages stubbed on the ' \
'same object and return multiple' do
expect_no_offenses(<<~RUBY)
before do
allow(Service).to receive(:foo).and_return(1, 2)
allow(Service).to receive(:bar).and_return(3, 4)
end
RUBY
end

it 'does not register an offense when multiple messeages stubbed on the ' \
'same object and message order' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 217a8dc

Please sign in to comment.