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

Add support for chain and multiple stub detection

  • Loading branch information...
pirj committed May 15, 2019
commit 20cd88a1a6ca3c5cae2c2340df4458081dcce16b
@@ -72,7 +72,9 @@ class SubjectStub < Cop

def_node_matcher :all_matcher?, '(send nil? :all ...)'

def_node_search :receive_message?, '(send nil? :receive ...)'
def_node_search :receive_message?, <<-PATTERN
(send nil? { :receive :receive_messages :receive_message_chain } ...)
PATTERN

def expectation?(node)
return if all_matcher?(node)
@@ -220,4 +220,32 @@
end
RUBY
end

it 'flags multiple-method stubs' do
expect_offense(<<-RUBY)
describe Foo do
subject(:foo) { described_class.new }
specify do
expect(foo).to receive_messages(bar: :baz, baz: :baz)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub your test subject.
expect(foo.bar).to eq(baz)
end
end
RUBY
end

it 'flags chain stubs' do
expect_offense(<<-RUBY)
describe Foo do
subject(:foo) { described_class.new }
specify do
expect(foo).to receive_message_chain(:bar, baz: :baz)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub your test subject.
expect(foo.bar.baz).to eq(baz)
end
end
RUBY
end
end
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.