-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #94
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Detect unreferenced `let!` calls being used for test setup | ||
# | ||
# @example | ||
# # Bad | ||
# | ||
# let!(:my_widget) { create(:widget) } | ||
# | ||
# it "..." do | ||
# expect(Widget.count).to eq(1) | ||
# end | ||
# | ||
# # Good | ||
# | ||
# it "..." do | ||
# create(:widget) | ||
# expect(Widget.count).to eq(1) | ||
# end | ||
# | ||
# before { create(:widget) } | ||
# | ||
# it "..." do | ||
# expect(Widget.count).to eq(1) | ||
# end | ||
class LetSetup < Cop | ||
include RuboCop::RSpec::TopLevelDescribe, RuboCop::RSpec::Language | ||
|
||
MSG = 'Do not use `let!` for test setup.'.freeze | ||
|
||
def_node_search :let_bang, '(block $(send nil :let! (sym $_)) args ...)' | ||
|
||
def_node_search :method_called?, '(send nil %)' | ||
|
||
def_node_matcher :example_group?, <<-PATTERN | ||
(block (send _ {#{ExampleGroups::ALL.to_node_pattern}} ...) ...) | ||
PATTERN | ||
|
||
def on_block(node) | ||
return unless example_group?(node) | ||
|
||
unused_let_bang(node) do |let| | ||
add_offense(let, :expression) | ||
end | ||
end | ||
|
||
private | ||
|
||
def unused_let_bang(node) | ||
let_bang(node) do |method_send, method_name| | ||
yield(method_send) unless method_called?(node, method_name) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
describe RuboCop::Cop::RSpec::LetSetup do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'complains when let! is used and not referenced' do | ||
expect_violation(<<-RUBY) | ||
describe Foo do | ||
let!(:foo) { bar } | ||
^^^^^^^^^^ Do not use `let!` for test setup. | ||
it 'does not use foo' do | ||
expect(baz).to eq(qux) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'ignores let! when used in `before`' do | ||
expect_no_violations(<<-RUBY) | ||
describe Foo do | ||
let!(:foo) { bar } | ||
before do | ||
foo | ||
end | ||
it 'does not use foo' do | ||
expect(baz).to eq(qux) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'ignores let! when used in example' do | ||
expect_no_violations(<<-RUBY) | ||
describe Foo do | ||
let!(:foo) { bar } | ||
it 'uses foo' do | ||
foo | ||
expect(baz).to eq(qux) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'complains when let! is used and not referenced within nested group' do | ||
expect_violation(<<-RUBY) | ||
describe Foo do | ||
context 'when something special happens' do | ||
let!(:foo) { bar } | ||
^^^^^^^^^^ Do not use `let!` for test setup. | ||
it 'does not use foo' do | ||
expect(baz).to eq(qux) | ||
end | ||
end | ||
it 'references some other foo' do | ||
foo | ||
end | ||
end | ||
RUBY | ||
end | ||
end |