Sha256: 560fa76a0281073cf2ea9666f5689a81c726159f7fa89fa9254553c300f06b06

Contents?: true

Size: 1.96 KB

Versions: 16

Compression:

Stored size: 1.96 KB

Contents

RSpec.describe RuboCop::Cop::RSpec::ExpectChange, :config do
  subject(:cop) { described_class.new(config) }

  let(:cop_config) do
    { 'EnforcedStyle' => enforced_style }
  end

  context 'with EnforcedStyle `method_call`' do
    let(:enforced_style) { 'method_call' }

    it 'finds blocks that contain simple message sending' do
      expect_offense(<<-RUBY)
        it do
          expect(run).to change { User.count }
                         ^^^^^^^^^^^^^^^^^^^^^ Prefer `change(User, :count)`.
        end
      RUBY
    end

    it 'ignores blocks that cannot be converted to obj/attribute pair' do
      expect_no_offenses(<<-RUBY)
        it do
          expect(run).to change { User.sum(:points) }
        end
      RUBY
    end

    it 'ignores change method of object that happens to receive a block' do
      expect_no_offenses(<<-RUBY)
        it do
          Record.change { User.count }
        end
      RUBY
    end

    include_examples(
      'autocorrect',
      'expect(run).to change { User.count }.by(1)',
      'expect(run).to change(User, :count).by(1)'
    )
  end

  context 'with EnforcedStyle `block`' do
    let(:enforced_style) { 'block' }

    it 'finds change matcher without block' do
      expect_offense(<<-RUBY)
        it do
          expect(run).to change(User, :count)
                         ^^^^^^^^^^^^^^^^^^^^ Prefer `change { User.count }`.
        end
      RUBY
    end

    it 'finds change matcher when receiver is a variable' do
      expect_offense(<<-RUBY)
        it do
          expect(run).to change(user, :count)
                         ^^^^^^^^^^^^^^^^^^^^ Prefer `change { user.count }`.
        end
      RUBY
    end

    it 'ignores methods called change' do
      expect_no_offenses(<<-RUBY)
        it do
          record.change(user, :count)
        end
      RUBY
    end

    include_examples(
      'autocorrect',
      'expect(run).to change(User, :count).by(1)',
      'expect(run).to change { User.count }.by(1)'
    )
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rubocop-rspec-1.32.0 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.31.0 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.30.1 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.30.0 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.29.1 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.29.0 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.28.0 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.27.0 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.26.0 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.25.1 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.25.0 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.24.0 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.23.0 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.22.2 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.22.1 spec/rubocop/cop/rspec/expect_change_spec.rb
rubocop-rspec-1.22.0 spec/rubocop/cop/rspec/expect_change_spec.rb