Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b32950b

Browse files
committed
Fix OverwritingSetup raising an error for unnamed subject
1 parent 5a2c8a0 commit b32950b

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

lib/rubocop/cop/rspec/overwriting_setup.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ def on_block(node)
4545
def find_duplicates(node)
4646
setup_expressions = Set.new
4747
node.each_child_node do |child|
48-
setup?(child) do
49-
name = one(child.send_node.arguments).value
50-
yield child, name unless setup_expressions.add?(name)
51-
end
48+
next unless setup?(child)
49+
50+
name = if child.send_node.arguments?
51+
child.send_node.first_argument.value
52+
else
53+
:subject
54+
end
55+
56+
yield child, name unless setup_expressions.add?(name)
5257
end
5358
end
5459
end

spec/rubocop/cop/rspec/overwriting_setup_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@
2222
RUBY
2323
end
2424

25+
it 'works with `subject!` and `let!`' do
26+
expect_offense(<<-RUBY)
27+
RSpec.describe User do
28+
subject!(:a) { a }
29+
30+
let!(:a) { b }
31+
^^^^^^^^^^^^^^ `a` is already defined.
32+
end
33+
RUBY
34+
end
35+
2536
it 'finds `let!` overwriting `let`' do
2637
expect_offense(<<-RUBY)
2738
RSpec.describe User do
@@ -44,6 +55,17 @@
4455
RUBY
4556
end
4657

58+
it 'handles unnamed subjects' do
59+
expect_offense(<<-RUBY)
60+
RSpec.describe User do
61+
subject { a }
62+
63+
let(:subject) { b }
64+
^^^^^^^^^^^^^^^^^^^ `subject` is already defined.
65+
end
66+
RUBY
67+
end
68+
4769
it 'does not encounter an error when handling an empty describe' do
4870
expect { inspect_source('RSpec.describe(User) do end', 'a_spec.rb') }
4971
.not_to raise_error

0 commit comments

Comments
 (0)