Sha256: b49e1c6a1f53c6a59857aa039efe8365a5b93d10c36e5d0870b99502b539611e

Contents?: true

Size: 1.12 KB

Versions: 6

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # This cop checks for `assert_raises` has an assertion method at
      # the bottom of block because the assertion will be never reached.
      #
      # @example
      #
      #   # bad
      #   assert_raises FooError do
      #     obj.occur_error
      #     assert_equal('foo', obj.bar) # Never asserted.
      #   end
      #
      #   # good
      #   assert_raises FooError do
      #     obj.occur_error
      #   end
      #   assert_equal('foo', obj.bar)
      #
      class UnreachableAssertion < Base
        include MinitestExplorationHelpers

        MSG = 'Unreachable `%<assertion_method>s` detected.'

        def on_block(node)
          return unless node.method?(:assert_raises) && (body = node.body)

          last_node = body.begin_type? ? body.children.last : body
          return unless last_node.send_type?

          method_name = last_node.method_name
          return unless assertion_method?(method_name)

          add_offense(last_node, message: format(MSG, assertion_method: method_name))
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-minitest-0.17.0 lib/rubocop/cop/minitest/unreachable_assertion.rb
rubocop-minitest-0.16.0 lib/rubocop/cop/minitest/unreachable_assertion.rb
rubocop-minitest-0.15.2 lib/rubocop/cop/minitest/unreachable_assertion.rb
rubocop-minitest-0.15.1 lib/rubocop/cop/minitest/unreachable_assertion.rb
rubocop-minitest-0.15.0 lib/rubocop/cop/minitest/unreachable_assertion.rb
rubocop-minitest-0.14.0 lib/rubocop/cop/minitest/unreachable_assertion.rb