Module: RuboCop::Minitest::AssertOffense

Included in:
TestCase
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.

The cop under test is derived from the test class name: FooTest resolves the Foo constant in the test class's namespace, then RuboCop::Cop::Minitest::Foo. Define a cop_class method in the test class to specify the cop explicitly:

class AssertNilTest < RuboCop::TestCase
private

def cop_class
  RuboCop::Cop::Minitest::AssertNil
end
end

The configuration for the cop under test and the target Ruby version can be specified by overriding cop_config, other_cops, or target_ruby_version in the test class:

To change them for a single test, assign them in the test method. This makes it possible for tests targeting different Ruby versions to live in the same test class:

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

cop_config and target_ruby_version


class MultipleAssertionsTest < RuboCop::TestCase
  private

  def cop_config
    { 'Max' => 1 }
  end

  def target_ruby_version
    3.0
  end
end

assigning target_ruby_version per test


class MyCopTest < RuboCop::TestCase
  def test_registers_offense_on_ruby31
    self.target_ruby_version = 3.1

    assert_offense(<<~RUBY)
      ...
    RUBY
  end

  def test_does_not_register_offense_on_ruby30
    self.target_ruby_version = 3.0

    assert_no_offenses(<<~RUBY)
      ...
    RUBY
  end
end

Class Method Summary collapse

Class Method Details

.integrate_plugins!Object

Makes the default configuration of extensions registered as lint_roller plugins visible through RuboCop::ConfigLoader.default_configuration. Guarded by a mutex so that parallel test threads reaching their first assertion at the same time do not integrate plugins twice or race on the lazy initialization of the default configuration.



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rubocop/minitest/assert_offense.rb', line 139

def integrate_plugins!
  PLUGIN_INTEGRATION_MUTEX.synchronize do
    next if @integrated_plugins

    plugins = Gem.loaded_specs.filter_map do |feature_name, feature_specification|
      feature_name if feature_specification.['default_lint_roller_plugin']
    end
    RuboCop::Plugin.integrate_plugins(RuboCop::Config.new, plugins)

    @integrated_plugins = true
  end
end