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
Prev

Improve detection of unnamed subject stubbing

  • Loading branch information...
pirj committed Jun 10, 2019
commit 05bf0c7926b5cfdf83934badf04ec12fd31a11db
@@ -61,7 +61,7 @@ class SubjectStub < Cop
def_node_matcher :message_expectation?, <<-PATTERN
(send
{
(send nil? { :expect :allow } (send nil? %))
(send nil? { :expect :allow } (send nil? {% :subject}))
(send nil? :is_expected)
}
#{Runners::ALL.node_pattern_union}
@@ -43,6 +43,32 @@
RUBY
end

it 'flags when an unnamed subject is mocked' do
expect_offense(<<-RUBY)
describe Foo do
subject { described_class.new }
it 'uses unnamed subject' do
expect(subject).to receive(:bar)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub methods of the object under test.
end
end
RUBY
end

it 'flags an expectation made on an unnamed subject' do
expect_offense(<<-RUBY)
describe Foo do
subject(:foo) { described_class.new }
it 'uses unnamed subject' do
expect(subject).to receive(:bar)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub methods of the object under test.
end
end
RUBY
end

it 'flags one-line expectcation syntax' do
expect_offense(<<-RUBY)
describe Foo do
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.