Sha256: 0db092ce4cef98fb12776141a164298bab29bbca76ed2255961b101b47f76ae4

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

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

  context 'without configuration' do
    let(:cop_config) { Hash.new }

    it 'flags multiple expectations' do
      expect_violation(<<-RUBY)
        describe Foo do
          it 'uses expect twice' do
          ^^^^^^^^^^^^^^^^^^^^^^ Too many expectations.
            expect(foo).to eq(bar)
            expect(baz).to eq(bar)
          end
        end
      RUBY
    end

    it 'approves of one expectation per example' do
      expect_no_violations(<<-RUBY)
        describe Foo do
          it 'does something neat' do
            expect(neat).to be(true)
          end

          it 'does something cool' do
            expect(cool).to be(true)
          end
        end
      RUBY
    end
  end

  context 'with configuration' do
    let(:cop_config) do
      { 'Max' => '2' }
    end

    it 'permits two expectations' do
      expect_no_violations(<<-RUBY)
        describe Foo do
          it 'uses expect twice' do
            expect(foo).to eq(bar)
            expect(baz).to eq(bar)
          end
        end
      RUBY
    end

    it 'flags three expectations' do
      expect_violation(<<-RUBY)
        describe Foo do
          it 'uses expect three times' do
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Too many expectations.
            expect(foo).to eq(bar)
            expect(baz).to eq(bar)
            expect(qux).to eq(bar)
          end
        end
      RUBY
    end
  end

  it 'generates a todo based on the worst violation' do
    inspect_source(cop, <<-RUBY, 'spec/foo_spec.rb')
      describe Foo do
        it 'uses expect twice' do
          expect(foo).to eq(bar)
          expect(baz).to eq(bar)
        end

        it 'uses expect three times' do
          expect(foo).to eq(bar)
          expect(baz).to eq(bar)
          expect(qux).to eq(bar)
        end
      end
    RUBY

    expect(cop.config_to_allow_offenses).to eq('Max' => 3)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-rspec-1.9.1 spec/rubocop/cop/rspec/multiple_expectations_spec.rb
rubocop-rspec-1.9.0 spec/rubocop/cop/rspec/multiple_expectations_spec.rb
rubocop-rspec-1.8.0 spec/rubocop/cop/rspec/multiple_expectations_spec.rb
rubocop-rspec-1.7.0 spec/rubocop/cop/rspec/multiple_expectations_spec.rb