lib/rubocop/cop/rspec/subject_stub.rb in rubocop-rspec-1.33.0 vs lib/rubocop/cop/rspec/subject_stub.rb in rubocop-rspec-1.34.0
- old
+ new
@@ -4,10 +4,12 @@
module Cop
module RSpec
# Checks for stubbed test subjects.
#
# @see https://robots.thoughtbot.com/don-t-stub-the-system-under-test
+ # @see https://samphippen.com/introducing-rspec-smells-and-where-to-find-them#smell-1-stubject
+ # @see https://github.com/rubocop-hq/rspec-style-guide#dont-stub-subject
#
# @example
# # bad
# describe Foo do
# subject(:bar) { baz }
@@ -16,14 +18,12 @@
# allow(bar).to receive(:qux?).and_return(true)
# end
# end
#
class SubjectStub < Cop
- include RuboCop::RSpec::TopLevelDescribe
+ MSG = 'Do not stub methods of the object under test.'
- MSG = 'Do not stub your test subject.'
-
# @!method subject(node)
# Find a named or unnamed subject definition
#
# @example anonymous subject
# subject(parse('subject { foo }').ast) do |name|
@@ -54,34 +54,31 @@
# allow(foo).to receive(:bar).with(1).and_return(2)
# expect(foo).to receive(:bar)
# expect(foo).to receive(:bar).with(1)
# expect(foo).to receive(:bar).with(1).and_return(2)
#
- # @example source that not matches
- # expect(foo).to all(receive(:bar))
- #
def_node_matcher :message_expectation?, <<-PATTERN
- {
- (send nil? :allow (send nil? %))
- (send (send nil? :expect (send nil? %)) :to #expectation?)
- }
+ (send
+ {
+ (send nil? { :expect :allow } (send nil? {% :subject}))
+ (send nil? :is_expected)
+ }
+ #{Runners::ALL.node_pattern_union}
+ #message_expectation_matcher?
+ )
PATTERN
- def_node_matcher :all_matcher?, '(send nil? :all ...)'
+ def_node_search :message_expectation_matcher?, <<-PATTERN
+ (send nil? {
+ :receive :receive_messages :receive_message_chain :have_received
+ } ...)
+ PATTERN
- def_node_search :receive_message?, '(send nil? :receive ...)'
-
- def expectation?(node)
- return if all_matcher?(node)
-
- receive_message?(node)
- end
-
def on_block(node)
return unless example_group?(node)
find_subject_stub(node) do |stub|
- add_offense(stub, location: :expression)
+ add_offense(stub)
end
end
private