Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve RSpec/SubjectStub cop #770

Merged
merged 10 commits into from Jun 10, 2019

Improve stub detection

Previously, the following code would still be detected as an offence:

  allow(subject)

even though there are no message expectations.

Also, `is_expected` was not supported, e.g.:

  is_expected.to receive(:bar)

was not flagged.
  • Loading branch information...
pirj committed May 15, 2019
commit 8ea922c017a0db4ff5ad93778bbf9b66cb5955cd
@@ -60,10 +60,14 @@ class SubjectStub < Cop
# 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? %))
(send nil? :is_expected)
}
:to
#expectation?
)
PATTERN

def_node_matcher :all_matcher?, '(send nil? :all ...)'
@@ -10,7 +10,7 @@
before do
allow(foo).to receive(:bar).and_return(baz)
^^^^^^^^^^ Do not stub your test subject.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub your test subject.
end
it 'uses expect twice' do
@@ -43,6 +43,19 @@
RUBY
end

it 'flags one-line expectcation syntax' do
expect_offense(<<-RUBY)
describe Foo do
subject(:foo) { described_class.new }
it 'uses one-line expectation syntax' do
is_expected.to receive(:bar)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub your test subject.
end
end
RUBY
end

it 'ignores stub within context where subject name changed' do
expect_no_offenses(<<-RUBY)
describe Foo do
@@ -80,7 +93,7 @@
before do
allow(foo).to receive(:wow)
^^^^^^^^^^ Do not stub your test subject.
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub your test subject.
end
it 'tries to trick rubocop-rspec' do
@@ -119,7 +132,7 @@
context 'when I shake things up' do
before do
allow(foo).to receive(:wow)
^^^^^^^^^^ Do not stub your test subject.
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub your test subject.
end
it 'tries to trick rubocop-rspec' do
@@ -141,7 +154,7 @@
before do
allow(foo).to receive(:wow)
allow(bar).to receive(:wow)
^^^^^^^^^^ Do not stub your test subject.
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub your test subject.
end
it 'tries to trick rubocop-rspec' do
@@ -163,7 +176,7 @@
it 'still flags this test' do
allow(foo).to receive(:blah)
^^^^^^^^^^ Do not stub your test subject.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub your test subject.
end
end
RUBY
@@ -184,7 +197,7 @@
allow(foo).to receive(:wow)
allow(bar).to receive(:wow)
allow(baz).to receive(:wow)
^^^^^^^^^^ Do not stub your test subject.
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub your test subject.
end
end
end
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.