-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check that stubs aren't added to mocks
- Loading branch information
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks that stubs aren't added to mocks. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# expect(foo).to receive(:bar).with(42).and_return("hello world") | ||
# | ||
# # good (without spies) | ||
# allow(foo).to receive(:bar).with(42).and_return("hello world") | ||
# expect(foo).to receive(:bar).with(42) | ||
# | ||
# # good (with spies) | ||
# allow(foo).to receive(:bar).with(42).and_return("hello world") | ||
# expect(foo).to have_received(:bar).with(42) | ||
# | ||
class MockNotStub < Cop | ||
MSG = "Don't stub your mock.".freeze | ||
|
||
def_node_matcher :message_expectation_with_return_argument, <<-PATTERN | ||
(send | ||
(send nil? :expect ...) :to | ||
$(send #receive :and_return _) | ||
) | ||
PATTERN | ||
|
||
def_node_matcher :message_expectation_with_return_block, <<-PATTERN | ||
(send | ||
(send nil? :expect ...) :to | ||
$(block #receive (args) _) | ||
) | ||
PATTERN | ||
|
||
def_node_matcher :receive, <<-PATTERN | ||
{ | ||
(send nil? :receive ...) | ||
(send (send nil? :receive ...) :with ...) | ||
} | ||
PATTERN | ||
|
||
def on_send(node) | ||
message_expectation_with_return_argument(node) do |match| | ||
add_offense(match, location: offending_argument_range(match.loc)) | ||
end | ||
|
||
message_expectation_with_return_block(node) do |match| | ||
add_offense(match, location: offending_block_range(match.loc)) | ||
end | ||
end | ||
|
||
def offending_argument_range(source_map) | ||
Parser::Source::Range.new( | ||
source_map.expression.source_buffer, | ||
source_map.dot.begin_pos, | ||
source_map.end.end_pos | ||
) | ||
end | ||
|
||
def offending_block_range(source_map) | ||
Parser::Source::Range.new( | ||
source_map.expression.source_buffer, | ||
source_map.begin.begin_pos, | ||
source_map.end.end_pos | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::MockNotStub do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'flags expect(...).to receive(...).with(...).and_return' do | ||
expect_offense(<<-RUBY) | ||
expect(foo).to receive(:bar).with(42).and_return("hello world") | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't stub your mock. | ||
RUBY | ||
end | ||
|
||
it 'flags expect(...).to receive(...).with(...) { } ' do | ||
expect_offense(<<-RUBY) | ||
expect(foo).to receive(:bar).with(42) { "hello world" } | ||
^^^^^^^^^^^^^^^^^ Don't stub your mock. | ||
RUBY | ||
end | ||
|
||
it 'flags expect(...).to receive(...).and_return' do | ||
expect_offense(<<-RUBY) | ||
expect(foo).to receive(:bar).and_return("hello world") | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't stub your mock. | ||
RUBY | ||
end | ||
|
||
it 'flags expect(...).to receive(...) { } ' do | ||
expect_offense(<<-RUBY) | ||
expect(foo).to receive(:bar) { "hello world" } | ||
^^^^^^^^^^^^^^^^^ Don't stub your mock. | ||
RUBY | ||
end | ||
|
||
it 'approves of expect(...).to have_received' do | ||
expect_no_offenses('expect(foo).to have_received(:bar)') | ||
end | ||
end |