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

Add test case for RSpec/RepeatedSubjectCall #1826

Merged
merged 1 commit into from Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 40 additions & 9 deletions spec/rubocop/cop/rspec/repeated_subject_call_spec.rb
@@ -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