Skip to content

Commit f58b9fe

Browse files
authored
Merge pull request #1994 from corsonknowles/use_simple_lookup_table_for_replacements
Add an else condition to correct `StubbedMock` behavior
2 parents 5d96600 + 2f4763e commit f58b9fe

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

lib/rubocop/cop/rspec/stubbed_mock.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ module RSpec
1414
# expect(foo).to receive(:bar).with(42)
1515
#
1616
class StubbedMock < Base
17-
MSG = 'Prefer `%<replacement>s` over `%<method_name>s` when ' \
17+
MSG = 'Prefer %<replacement>s over `%<method_name>s` when ' \
1818
'configuring a response.'
19+
RESTRICT_ON_SEND = %i[to].freeze
1920

2021
# @!method message_expectation?(node)
2122
# Match message expectation matcher
@@ -133,8 +134,6 @@ class StubbedMock < Base
133134
}
134135
PATTERN
135136

136-
RESTRICT_ON_SEND = %i[to].freeze
137-
138137
def on_send(node)
139138
expectation(node) do |expectation, method_name, matcher|
140139
on_expectation(expectation, method_name, matcher)
@@ -155,19 +154,23 @@ def on_expectation(expectation, method_name, matcher)
155154
end
156155

157156
def msg(method_name)
158-
format(MSG,
159-
method_name: method_name,
160-
replacement: replacement(method_name))
157+
format(
158+
MSG,
159+
method_name: method_name,
160+
replacement: replacement(method_name)
161+
)
161162
end
162163

163164
def replacement(method_name)
164165
case method_name
165166
when :expect
166-
:allow
167+
'`allow`'
167168
when :is_expected
168-
'allow(subject)'
169+
'`allow(subject)`'
169170
when :expect_any_instance_of
170-
:allow_any_instance_of
171+
'`allow_any_instance_of`'
172+
else
173+
'an allow statement'
171174
end
172175
end
173176
end

spec/rubocop/cop/rspec/stubbed_mock_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,19 @@
126126
RUBY
127127
end
128128

129-
it 'tolerates passed arguments without parentheses' do
129+
it 'flags even when passed arguments without parentheses' do
130130
expect_offense(<<~RUBY)
131131
expect(Foo)
132132
^^^^^^^^^^^ Prefer `allow` over `expect` when configuring a response.
133133
.to receive(:new)
134134
.with(bar).and_return baz
135135
RUBY
136136
end
137+
138+
it 'flags `are_expected`' do
139+
expect_offense(<<~RUBY)
140+
are_expected.to receive(:bar).and_return(:baz)
141+
^^^^^^^^^^^^ Prefer an allow statement over `are_expected` when configuring a response.
142+
RUBY
143+
end
137144
end

0 commit comments

Comments
 (0)