Skip to content

Minitest/RefuteInDelta (& Minitest/AssertInDelta?) not safe for autocorrect #62

@MatzFan

Description

@MatzFan

.rubocop.yml

---
require:
  - rubocop-minitest

AllCops:
  NewCops: enable

test/test_minitest_assert_in_delta.rb

# frozen_string_literal: true

require 'minitest/autorun'

# test
class AssertInDeltaTest < Minitest::Test
  def test_assert_in_delta_cop
    refute_equal(4.2, 420_001 / 100_000.0)
  end
end

$ ruby test/test_minitest_assert_in_delta.rb
=> pass

$ rubocop

Offenses:

test/test_minitest_assert_in_delta.rb:10:5: C: [Correctable] Minitest/RefuteInDelta: Prefer using refute_in_delta(4.2, 420_001 / 100_000.0).
    refute_equal(4.2, 420_001 / 100_000.0)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable

$ rubocop -a
=> autocorrected

$ ruby test/test_minitest_assert_in_delta.rb
=> fail

Fails because the default delta value for refute_in_delta is 0.001. As Rubocop can never know the user's intended delta value this cop is not safe for autocorrect.

Don't know whether you'd consider Minitest/RefuteInDelta unsafe also, as if you replace the assertion in the above example with assert_equal, autocorrect turns a failing test into one that passes..

I'd also suggest the suggestion text for both cops use the good code examples where an explicit delta value is provided.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions