Sha256: 087f857d8241723853fe2e01a4f89fcd6bc9311ea68d2eefafaf19019354281e

Contents?: true

Size: 1.61 KB

Versions: 12

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for a specified error in checking raised errors.
      #
      # Enforces one of an Exception type, a string, or a regular
      # expression to match against the exception message as a parameter
      # to `raise_error`
      #
      # @example
      #
      #     # bad
      #     expect {
      #       raise StandardError.new('error')
      #     }.to raise_error
      #
      #     # good
      #     expect {
      #       raise StandardError.new('error')
      #     }.to raise_error(StandardError)
      #
      #     expect {
      #       raise StandardError.new('error')
      #     }.to raise_error('error')
      #
      #     expect {
      #       raise StandardError.new('error')
      #     }.to raise_error(/err/)
      #
      #     expect { do_something }.not_to raise_error
      class UnspecifiedException < Cop
        MSG = 'Specify the exception being captured'

        def_node_matcher :empty_raise_error_or_exception, <<-PATTERN
          (send
            (block
                (send nil? :expect) ...)
            :to
            (send nil? {:raise_error :raise_exception})
          )
        PATTERN

        def on_send(node)
          return unless empty_exception_matcher?(node)

          add_offense(node.children.last)
        end

        def empty_exception_matcher?(node)
          empty_raise_error_or_exception(node) && !block_with_args?(node.parent)
        end

        def block_with_args?(node)
          return unless node&.block_type?

          node.arguments?
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rubocop-rspec-1.42.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-1.41.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-1.40.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-1.39.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-1.38.1 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-1.38.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-1.37.1 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-1.37.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-1.36.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-1.35.0 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-1.34.1 lib/rubocop/cop/rspec/unspecified_exception.rb
rubocop-rspec-1.34.0 lib/rubocop/cop/rspec/unspecified_exception.rb