Module: RuboCop::Minitest::AssertOffense

Defined in:
lib/rubocop/minitest/assert_offense.rb

Overview

Mixin for ‘assert_offense` and `assert_no_offenses`

This mixin makes it easier to specify strict offense assertions in a declarative and visual fashion. Just type out the code that should generate an offense, annotate code by writing ‘^’s underneath each character that should be highlighted, and follow the carets with a string (separated by a space) that is the message of the offense. You can include multiple offenses in one code snippet.

Autocorrection can be tested using ‘assert_correction` after `assert_offense`.

If you do not want to specify an offense then use the companion method ‘assert_no_offenses`. This method is a much simpler assertion since it just inspects the source and checks that there were no offenses. The `assert_offense` method has to do more work by parsing out lines that contain carets.

If the code produces an offense that could not be autocorrected, you can use ‘assert_no_corrections` after `assert_offense`.

rubocop:disable Metrics/ModuleLength

Examples:

Usage


assert_offense(<<~RUBY)
  class FooTest < Minitest::Test
    def test_do_something
      assert_equal(nil, somestuff)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
    end
  end
RUBY

‘assert_offense` and `assert_correction`


assert_offense(<<~RUBY)
  class FooTest < Minitest::Test
    def test_do_something
      assert_equal(nil, somestuff)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
    end
  end
RUBY

assert_correction(<<~RUBY)
  class FooTest < Minitest::Test
    def test_do_something
      assert_nil(somestuff)
    end
  end
RUBY

‘assert_offense` and `assert_no_corrections`


assert_offense(<<~RUBY)
  class FooTest < Minitest::Test
    def test_do_something
      assert_equal(nil, somestuff)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
    end
  end
RUBY

assert_no_corrections