Skip to content

Commit

Permalink
Add test case for RSpec/RepeatedSubjectCall
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Mar 1, 2024
1 parent 6b37836 commit 0d5b4d2
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions spec/rubocop/cop/rspec/repeated_subject_call_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpec::RepeatedSubjectCall do
it 'registers an offense for a singular block' do
it 'registers an offense when a singular block' do
expect_offense(<<-RUBY)
RSpec.describe Foo do
it do
Expand All @@ -13,7 +13,7 @@
RUBY
end

it 'registers an offense for repeated blocks' do
it 'registers an offense when repeated blocks' do
expect_offense(<<-RUBY)
RSpec.describe Foo do
it do
Expand All @@ -25,7 +25,7 @@
RUBY
end

it 'registers an offense for nested blocks' do
it 'registers an offense when nested blocks' do
expect_offense(<<-RUBY)
RSpec.describe Foo do
it do
Expand All @@ -39,7 +39,7 @@
RUBY
end

it 'registers an offense for custom subjects' do
it 'registers an offense when custom subjects' do
expect_offense(<<-RUBY)
RSpec.describe Foo do
subject(:bar) { do_something }
Expand All @@ -53,7 +53,7 @@
RUBY
end

it 'registers no offenses for no block' do
it 'does not register an offense when no block' do
expect_no_offenses(<<~RUBY)
RSpec.describe Foo do
it do
Expand All @@ -64,7 +64,7 @@
RUBY
end

it 'registers no offenses for block first' do
it 'does not register an offense when block first' do
expect_no_offenses(<<~RUBY)
RSpec.describe Foo do
it do
Expand All @@ -75,7 +75,7 @@
RUBY
end

it 'registers no offenses for different subjects' do
it 'does not register an offense when different subjects' do
expect_no_offenses(<<-RUBY)
RSpec.describe Foo do
subject { do_something_else }
Expand All @@ -89,7 +89,7 @@
RUBY
end

it 'registers no offenses for multiple no description it blocks' do
it 'does not register an offense when multiple no description it blocks' do
expect_no_offenses(<<-RUBY)
RSpec.describe Foo do
it do
Expand All @@ -103,7 +103,7 @@
RUBY
end

it 'registers no offenses for `subject.method_call`' do
it 'does not register an offense when `subject.method_call`' do
expect_no_offenses(<<~RUBY)
RSpec.describe Foo do
it do
Expand All @@ -113,4 +113,35 @@
end
RUBY
end

it 'does not register an offense when `subject` with not expectation' do
expect_no_offenses(<<~RUBY)
RSpec.describe Foo do
it do
allow(Foo).to receive(:bar).and_return(subject)
allow(Foo).to receive(:bar) { subject }
end
end
RUBY
end

it 'does not register an offense when `subject` not inside example' do
expect_no_offenses(<<~RUBY)
RSpec.describe Foo do
subject { do_something }
it do
expect { subject }.to change { Foo.count }
end
end
RUBY
end

it 'does not register an offense when `subject` is not inside describe' do
expect_no_offenses(<<~RUBY)
Foo.subject(:bar)
subject(:bar)
subject
RUBY
end
end

0 comments on commit 0d5b4d2

Please sign in to comment.