summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/rspec/be_success_matcher_spec.rb
blob: 17408a696def891e75c4fee8f4d293b13b1219aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true

require 'spec_helper'

require_relative '../../../../rubocop/cop/rspec/be_success_matcher'

describe RuboCop::Cop::RSpec::BeSuccessMatcher do
  include CopHelper

  let(:source_file) { 'spec/foo_spec.rb' }

  subject(:cop) { described_class.new }

  shared_examples 'cop' do |good:, bad:|
    context "using #{bad} call" do
      it "registers an offense for `#{bad}`" do
        inspect_source(bad, source_file)

        expect(cop.offenses.size).to eq(1)
        expect(cop.offenses.map(&:line)).to eq([1])
        expect(cop.highlights).to eq([bad])
      end

      it "registers an offense for `#{bad}` and autocorrects it to `#{good}`" do
        autocorrected = autocorrect_source(bad, source_file)

        expect(autocorrected).to eql(good)
      end
    end

    context "using #{good} call" do
      it 'does not register an offense' do
        inspect_source(good)

        expect(cop.offenses).to be_empty
      end
    end
  end

  it_behaves_like 'cop',
    bad: 'expect(response).to be_success',
    good: 'expect(response).to be_successful'

  it_behaves_like 'cop',
    bad: 'expect(response).to_not be_success',
    good: 'expect(response).to_not be_successful'

  it_behaves_like 'cop',
    bad: 'expect(response).not_to be_success',
    good: 'expect(response).not_to be_successful'

  it_behaves_like 'cop',
    bad: 'is_expected.to be_success',
    good: 'is_expected.to be_successful'

  it_behaves_like 'cop',
    bad: 'is_expected.to_not be_success',
    good: 'is_expected.to_not be_successful'

  it_behaves_like 'cop',
    bad: 'is_expected.not_to be_success',
    good: 'is_expected.not_to be_successful'
end