Skip to content

Commit

Permalink
Merge pull request #187 from bquorning/message-chain-cop
Browse files Browse the repository at this point in the history
Add MessageChain cop disallowing stubbed chains
  • Loading branch information
backus authored Aug 23, 2016
2 parents a2517ee + 8029cac commit 5620453
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Add `BeEql` cop which looks for expectations that can use `be(...)` instead of `eql(...)`. ([@backus][])
* Add autocorrect support for `BeEql` cop. ([@backus][])
* Add `MessageExpectation` cop for enforcing consistent style of either `expect(...).to receive` or `allow(...).to receive`. ([@backus][])
* Add `MessageChain` cop. ([@bquorning][])

## 1.6.0 (2016-08-03)

Expand Down
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ RSpec/ExpectActual:
Description: Checks for `expect(...)` calls containing literal values.
Enabled: true

RSpec/MessageChain:
Description: Check that chains of messages are not being stubbed.
Enabled: true

RSpec/MultipleDescribes:
Description: Checks for multiple top level describes.
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop-rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
require 'rubocop/cop/rspec/instance_variable'
require 'rubocop/cop/rspec/leading_subject'
require 'rubocop/cop/rspec/let_setup'
require 'rubocop/cop/rspec/message_chain'
require 'rubocop/cop/rspec/message_expectation'
require 'rubocop/cop/rspec/multiple_describes'
require 'rubocop/cop/rspec/multiple_expectations'
Expand Down
33 changes: 33 additions & 0 deletions lib/rubocop/cop/rspec/message_chain.rb
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
21 changes: 21 additions & 0 deletions spec/rubocop/cop/rspec/message_chain_spec.rb
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

0 comments on commit 5620453

Please sign in to comment.