rubocop / minitest-style-guide

Best practices for writing your tests
https://minitest.rubystyle.guide
70 stars 15 forks source link

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

Open MatzFan opened 2 months ago

MatzFan commented 2 months ago

.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.