-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from bquorning/message-chain-cop
Add MessageChain cop disallowing stubbed chains
- Loading branch information
Showing
5 changed files
with
60 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
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,33 @@ | ||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Check that chains of messages are not being stubbed. | ||
# | ||
# @example | ||
# # bad | ||
# allow(foo).to receive_message_chain(:bar, :baz).and_return(42) | ||
# | ||
# # better | ||
# thing = Thing.new(baz: 42) | ||
# allow(foo).to receive(bar: thing) | ||
# | ||
class MessageChain < Cop | ||
include RuboCop::RSpec::SpecOnly | ||
|
||
MESSAGE = 'Avoid stubbing using `%<method>s`'.freeze | ||
|
||
MESSAGE_CHAIN_METHODS = [ | ||
:receive_message_chain, | ||
:stub_chain | ||
].freeze | ||
|
||
def on_send(node) | ||
_receiver, method_name, *_args = *node | ||
return unless MESSAGE_CHAIN_METHODS.include?(method_name) | ||
|
||
add_offense(node, :selector, MESSAGE % { method: method_name }) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
describe RuboCop::Cop::RSpec::MessageChain do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'finds `receive_message_chain`' do | ||
expect_violation(<<-RUBY) | ||
before do | ||
allow(foo).to receive_message_chain(:one, :two) { :three } | ||
^^^^^^^^^^^^^^^^^^^^^ Avoid stubbing using `receive_message_chain` | ||
end | ||
RUBY | ||
end | ||
|
||
it 'finds old `stub_chain` syntax' do | ||
expect_violation(<<-RUBY) | ||
before do | ||
foo.stub_chain(:one, :two).and_return(:three) | ||
^^^^^^^^^^ Avoid stubbing using `stub_chain` | ||
end | ||
RUBY | ||
end | ||
end |