summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/rspec/be_success_matcher_spec.rb
blob: fcb5791ef57e8eefb5507f1facd6c9f2f1299ebd (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'spec_helper'

require 'rubocop'
require 'rubocop/rspec/support'

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

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

  OFFENSE_CALL_EXPECT_TO_BE_SUCCESS = %(expect(response).to be_success).freeze
  OFFENSE_CALL_IS_EXPECTED_TO_BE_SUCCESS = %(is_expected.to be_success).freeze
  CALL_EXPECT_TO_BE_SUCCESSFUL = %(expect(response).to be_successful).freeze
  CALL_IS_EXPECTED_TO_BE_SUCCESSFUL = %(is_expected.to be_successful).freeze

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

  subject(:cop) { described_class.new }

  shared_examples 'an offensive be_success call' do |content|
    it "registers an offense for `#{content}`" do
      inspect_source(content, source_file)

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

  context 'in a controller spec file' do
    before do
      allow(cop).to receive(:in_controller_spec?).and_return(true)
    end

    context "using expect(response).to be_success call" do
      it_behaves_like 'an offensive be_success call', OFFENSE_CALL_EXPECT_TO_BE_SUCCESS
    end

    context "using is_expected.to be_success call" do
      it_behaves_like 'an offensive be_success call', OFFENSE_CALL_IS_EXPECTED_TO_BE_SUCCESS
    end

    context "using expect(response).to be_successful" do
      it "does not register an offense" do
        inspect_source(CALL_EXPECT_TO_BE_SUCCESSFUL)

        expect(cop.offenses.size).to eq(0)
      end
    end

    context "using is_expected.to be_successful" do
      it "does not register an offense" do
        inspect_source(CALL_IS_EXPECTED_TO_BE_SUCCESSFUL)

        expect(cop.offenses.size).to eq(0)
      end
    end
  end

  context 'outside of a controller spec file' do
    context "using expect(response).to be_success call" do
      it "does not register an offense" do
        inspect_source(OFFENSE_CALL_EXPECT_TO_BE_SUCCESS)

        expect(cop.offenses.size).to eq(0)
      end
    end

    context "using is_expected.to be_success call" do
      it "does not register an offense" do
        inspect_source(OFFENSE_CALL_IS_EXPECTED_TO_BE_SUCCESS)

        expect(cop.offenses.size).to eq(0)
      end
    end
  end
end