Sha256: 6a9b089f698444f85a82c339d1d8962ec91f9c8d13b72fc0bf32881d2cd8705d

Contents?: true

Size: 1.44 KB

Versions: 12

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Using `ActionController::TestCase`` is discouraged and should be replaced by
      # `ActionDispatch::IntegrationTest``. Controller tests are too close to the
      # internals of a controller whereas integration tests mimic the browser/user.
      #
      # @safety
      #   This cop's autocorrection is unsafe because the API of each test case class is different.
      #   Make sure to update each test of your controller test cases after changing the superclass.
      #
      # @example
      #   # bad
      #   class MyControllerTest < ActionController::TestCase
      #   end
      #
      #   # good
      #   class MyControllerTest < ActionDispatch::IntegrationTest
      #   end
      #
      class ActionControllerTestCase < Base
        extend AutoCorrector
        extend TargetRailsVersion

        MSG = 'Use `ActionDispatch::IntegrationTest` instead.'

        minimum_target_rails_version 5.0

        def_node_matcher :action_controller_test_case?, <<~PATTERN
          (class
            (const _ _)
            (const (const {nil? cbase} :ActionController) :TestCase) _)
        PATTERN

        def on_class(node)
          return unless action_controller_test_case?(node)

          add_offense(node.parent_class) do |corrector|
            corrector.replace(node.parent_class, 'ActionDispatch::IntegrationTest')
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 11 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/action_controller_test_case.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.20.2/lib/rubocop/cop/rails/action_controller_test_case.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/cop/rails/action_controller_test_case.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.20.2/lib/rubocop/cop/rails/action_controller_test_case.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/action_controller_test_case.rb
rubocop-rails-2.20.2 lib/rubocop/cop/rails/action_controller_test_case.rb
rubocop-rails-2.20.1 lib/rubocop/cop/rails/action_controller_test_case.rb
rubocop-rails-2.20.0 lib/rubocop/cop/rails/action_controller_test_case.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/cop/rails/action_controller_test_case.rb
rubocop-rails-2.19.1 lib/rubocop/cop/rails/action_controller_test_case.rb
rubocop-rails-2.19.0 lib/rubocop/cop/rails/action_controller_test_case.rb
rubocop-rails-2.18.0 lib/rubocop/cop/rails/action_controller_test_case.rb