Sha256: 0b379a15c9b50e3d8a100665a11bbaf4ac343e8f6d95328b9333a5dbd5994b49

Contents?: true

Size: 1.64 KB

Versions: 22

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpec::NamedSubject do
  subject(:cop) { described_class.new }

  it 'checks `it` and `specify` for explicit subject usage' do
    expect_offense(<<-RUBY)
      RSpec.describe User do
        subject { described_class.new }

        it "is valid" do
          expect(subject.valid?).to be(true)
                 ^^^^^^^ Name your test subject if you need to reference it explicitly.
        end

        specify do
          expect(subject.valid?).to be(true)
                 ^^^^^^^ Name your test subject if you need to reference it explicitly.
        end
      end
    RUBY
  end

  it 'checks before and after for explicit subject usage' do
    expect_offense(<<-RUBY)
      RSpec.describe User do
        subject { described_class.new }

        before(:each) do
          do_something_with(subject)
                            ^^^^^^^ Name your test subject if you need to reference it explicitly.
        end

        after do
          do_something_with(subject)
                            ^^^^^^^ Name your test subject if you need to reference it explicitly.
        end
      end
    RUBY
  end

  it 'checks around(:each) for explicit subject usage' do
    expect_offense(<<-RUBY)
      RSpec.describe User do
        subject { described_class.new }

        around(:each) do |test|
          do_something_with(subject)
                            ^^^^^^^ Name your test subject if you need to reference it explicitly.
        end
      end
    RUBY
  end

  it 'ignores subject when not wrapped inside a test' do
    expect_no_offenses(<<-RUBY)
      def foo
        it(subject)
      end
    RUBY
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
rubocop-rspec-1.30.1 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.30.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.29.1 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.29.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.28.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.27.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.26.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.25.1 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.25.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.24.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.23.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.22.2 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.22.1 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.22.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.21.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.20.1 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.20.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.19.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.18.0 spec/rubocop/cop/rspec/named_subject_spec.rb
rubocop-rspec-1.17.1 spec/rubocop/cop/rspec/named_subject_spec.rb