summaryrefslogtreecommitdiff
path: root/rubocop/cop/rspec/be_success_matcher.rb
blob: a137e2dba69959bca270871877da5f67bb1e0a53 (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
require_relative '../../spec_helpers'

module RuboCop
  module Cop
    module RSpec
      # This cop checks for `be_success` usage in controller specs
      #
      # @example
      #
      #   # bad
      #   it "responds with success" do
      #     expect(response).to be_success
      #   end
      #
      #   it { is_expected.to be_success }
      #
      #   # good
      #   it "responds with success" do
      #     expect(response).to be_successful
      #   end
      #
      #   it { is_expected.to be_successful }
      #
      class BeSuccessMatcher < RuboCop::Cop::Cop
        include SpecHelpers

        MESSAGE = "Don't use deprecated `success?` method, use `successful?` instead.".freeze

        def_node_search :expect_to_be_success?, <<~PATTERN
          (send (send nil? :expect (send nil? ...)) :to (send nil? :be_success))
        PATTERN

        def_node_search :is_expected_to_be_success?, <<~PATTERN
          (send (send nil? :is_expected) :to (send nil? :be_success))
        PATTERN

        def be_success_usage?(node)
          expect_to_be_success?(node) || is_expected_to_be_success?(node)
        end

        def on_send(node)
          return unless in_controller_spec?(node)
          return unless be_success_usage?(node)

          add_offense(node, location: :expression, message: MESSAGE)
        end
      end
    end
  end
end