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("class FooTest < Minitest::Test\n  def test_do_something\n    assert_equal(nil, somestuff)\n    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.\n  end\nend\n")

‘assert_offense` and `assert_correction`


assert_offense("class FooTest < Minitest::Test\n  def test_do_something\n    assert_equal(nil, somestuff)\n    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.\n  end\nend\n")

assert_correction("class FooTest < Minitest::Test\n  def test_do_something\n    assert_nil(somestuff)\n  end\nend\n")

‘assert_offense` and `assert_no_corrections`


assert_offense("class FooTest < Minitest::Test\n  def test_do_something\n    assert_equal(nil, somestuff)\n    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.\n  end\nend\n")

assert_no_corrections