Skip to content

Instantly share code, notes, and snippets.

@pirj pirj/subject_stub.rb
Last active May 20, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.