Skip to content

Commit dd6ae03

Browse files
authored
Merge pull request #1826 from rubocop/test-case
Add test case for `RSpec/RepeatedSubjectCall`
2 parents 6b37836 + 0d5b4d2 commit dd6ae03

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

spec/rubocop/cop/rspec/repeated_subject_call_spec.rb

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::RSpec::RepeatedSubjectCall do
4-
it 'registers an offense for a singular block' do
4+
it 'registers an offense when a singular block' do
55
expect_offense(<<-RUBY)
66
RSpec.describe Foo do
77
it do
@@ -13,7 +13,7 @@
1313
RUBY
1414
end
1515

16-
it 'registers an offense for repeated blocks' do
16+
it 'registers an offense when repeated blocks' do
1717
expect_offense(<<-RUBY)
1818
RSpec.describe Foo do
1919
it do
@@ -25,7 +25,7 @@
2525
RUBY
2626
end
2727

28-
it 'registers an offense for nested blocks' do
28+
it 'registers an offense when nested blocks' do
2929
expect_offense(<<-RUBY)
3030
RSpec.describe Foo do
3131
it do
@@ -39,7 +39,7 @@
3939
RUBY
4040
end
4141

42-
it 'registers an offense for custom subjects' do
42+
it 'registers an offense when custom subjects' do
4343
expect_offense(<<-RUBY)
4444
RSpec.describe Foo do
4545
subject(:bar) { do_something }
@@ -53,7 +53,7 @@
5353
RUBY
5454
end
5555

56-
it 'registers no offenses for no block' do
56+
it 'does not register an offense when no block' do
5757
expect_no_offenses(<<~RUBY)
5858
RSpec.describe Foo do
5959
it do
@@ -64,7 +64,7 @@
6464
RUBY
6565
end
6666

67-
it 'registers no offenses for block first' do
67+
it 'does not register an offense when block first' do
6868
expect_no_offenses(<<~RUBY)
6969
RSpec.describe Foo do
7070
it do
@@ -75,7 +75,7 @@
7575
RUBY
7676
end
7777

78-
it 'registers no offenses for different subjects' do
78+
it 'does not register an offense when different subjects' do
7979
expect_no_offenses(<<-RUBY)
8080
RSpec.describe Foo do
8181
subject { do_something_else }
@@ -89,7 +89,7 @@
8989
RUBY
9090
end
9191

92-
it 'registers no offenses for multiple no description it blocks' do
92+
it 'does not register an offense when multiple no description it blocks' do
9393
expect_no_offenses(<<-RUBY)
9494
RSpec.describe Foo do
9595
it do
@@ -103,7 +103,7 @@
103103
RUBY
104104
end
105105

106-
it 'registers no offenses for `subject.method_call`' do
106+
it 'does not register an offense when `subject.method_call`' do
107107
expect_no_offenses(<<~RUBY)
108108
RSpec.describe Foo do
109109
it do
@@ -113,4 +113,35 @@
113113
end
114114
RUBY
115115
end
116+
117+
it 'does not register an offense when `subject` with not expectation' do
118+
expect_no_offenses(<<~RUBY)
119+
RSpec.describe Foo do
120+
it do
121+
allow(Foo).to receive(:bar).and_return(subject)
122+
allow(Foo).to receive(:bar) { subject }
123+
end
124+
end
125+
RUBY
126+
end
127+
128+
it 'does not register an offense when `subject` not inside example' do
129+
expect_no_offenses(<<~RUBY)
130+
RSpec.describe Foo do
131+
subject { do_something }
132+
133+
it do
134+
expect { subject }.to change { Foo.count }
135+
end
136+
end
137+
RUBY
138+
end
139+
140+
it 'does not register an offense when `subject` is not inside describe' do
141+
expect_no_offenses(<<~RUBY)
142+
Foo.subject(:bar)
143+
subject(:bar)
144+
subject
145+
RUBY
146+
end
116147
end

0 commit comments

Comments
 (0)