Skip to content

Instantly share code, notes, and snippets.

@pirj
Last active May 20, 2019 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirj/810a918e74e74ff9224c471e834ab34a to your computer and use it in GitHub Desktop.
Save pirj/810a918e74e74ff9224c471e834ab34a to your computer and use it in GitHub Desktop.
Modified version of the `lib/rubocop/cop/rspec/subject_stub.rb` cop to detect `expect(subject).to all receive(...)` syntax only
def_node_matcher :message_expectation?, <<-PATTERN
(send
{
(send nil? { :expect :allow } (send nil? %))
(send nil? :is_expected)
}
#{Runners::ALL.node_pattern_union}
#all_matcher?
)
PATTERN
def_node_search :all_matcher?, <<-PATTERN
(send nil? :all #message_expectation_matcher?)
PATTERN
def_node_search :message_expectation_matcher?, <<-PATTERN
(send nil? {
:receive :receive_messages :receive_message_chain :have_received
} ... )
PATTERN
RSpec.describe 'a' do
subject(:a) {}
it { expect(a).to all receive(:b) }
it { expect(a).to all receive(:b).with(1) }
it { expect(a).to all receive(:b).with(1).and_sdfsd { 1 } }
it { expect(a).to receive(:c) }
it { expect(a).to receive(:b).with(1).and_sdfsd { 1 } }
end
spec/a_spec.rb:4:8: C: RSpec/SubjectStub: Do not stub methods of the object under test.
it { expect(a).to all receive(:b) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/a_spec.rb:5:8: C: RSpec/SubjectStub: Do not stub methods of the object under test.
it { expect(a).to all receive(:b).with(1) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/a_spec.rb:6:8: C: RSpec/SubjectStub: Do not stub methods of the object under test.
it { expect(a).to all receive(:b).with(1).and_sdfsd { 1 } }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment