freerange / mocha

A mocking and stubbing library for Ruby

Home Page:https://mocha.jamesmead.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

rake test fails with ruby 3.2.0preview3

mtasaka opened this issue · comments

With latest git mocha ( f4a858d ) rake test fails with ruby3.2preview3 like:

  1) Error:
RegexpMatchesTest#test_should_not_match_on_argument_that_does_not_respond_to_equals_tilde:
NameError: undefined method `=~' for class `#<Class:0x00007fbe4cb46b00>'
    /builddir/build/GIT/mocha/test/unit/parameter_matchers/regexp_matches_test.rb:41:in `block in test_should_not_match_on_argument_that_does_not_respond_to_equals_tilde'
    /builddir/build/GIT/mocha/test/unit/parameter_matchers/regexp_matches_test.rb:41:in `initialize'
    /builddir/build/GIT/mocha/test/unit/parameter_matchers/regexp_matches_test.rb:41:in `new'
    /builddir/build/GIT/mocha/test/unit/parameter_matchers/regexp_matches_test.rb:41:in `test_should_not_match_on_argument_that_does_not_respond_to_equals_tilde'

  2) Error:
RegexpMatchesTest#test_should_not_raise_error_on_argument_that_does_not_respond_to_equals_tilde:
NameError: undefined method `=~' for class `#<Class:0x00007fbe4cb45200>'
    /builddir/build/GIT/mocha/test/unit/parameter_matchers/regexp_matches_test.rb:35:in `block in test_should_not_raise_error_on_argument_that_does_not_respond_to_equals_tilde'
    /builddir/build/GIT/mocha/test/unit/parameter_matchers/regexp_matches_test.rb:35:in `initialize'
    /builddir/build/GIT/mocha/test/unit/parameter_matchers/regexp_matches_test.rb:35:in `new'
    /builddir/build/GIT/mocha/test/unit/parameter_matchers/regexp_matches_test.rb:35:in `test_should_not_raise_error_on_argument_that_does_not_respond_to_equals_tilde'

425 runs, 814 assertions, 0 failures, 2 errors, 0 skips

So test_should_not_raise_error_on_argument_that_does_not_respond_to_equals_tilde seems to test for object not responding to =~ , so it tries to create Class object then trying to undefine =~ method explicitly, however with ruby3.2 this makes error by:

https://github.com/ruby/ruby/blob/eaf2b6c4396ff19921cbc75a394d7a752aaab4ef/NEWS.md?plain=1#L306

Quick fix is:

diff --git a/test/unit/parameter_matchers/regexp_matches_test.rb b/test/unit/parameter_matchers/regexp_matches_test.rb
index e0282c8..9251054 100644
--- a/test/unit/parameter_matchers/regexp_matches_test.rb
+++ b/test/unit/parameter_matchers/regexp_matches_test.rb
@@ -32,13 +32,13 @@ class RegexpMatchesTest < Mocha::TestCase
   end
 
   def test_should_not_raise_error_on_argument_that_does_not_respond_to_equals_tilde
-    object_not_responding_to_equals_tilde = Class.new { undef =~ }.new
+    object_not_responding_to_equals_tilde = Class.new { undef =~ if respond_to?(:=~) }.new
     matcher = regexp_matches(/oo/)
     assert_nothing_raised { matcher.matches?([object_not_responding_to_equals_tilde]) }
   end
 
   def test_should_not_match_on_argument_that_does_not_respond_to_equals_tilde
-    object_not_responding_to_equals_tilde = Class.new { undef =~ }.new
+    object_not_responding_to_equals_tilde = Class.new { undef =~ if respond_to?(:=~) }.new
     matcher = regexp_matches(/oo/)
     assert !matcher.matches?([object_not_responding_to_equals_tilde])
   end

@mtasaka Thanks for reporting this. I will apply the patch you have suggested as soon as I have time.

Note to self: the change is explained in more detail here.

@mtasaka Sorry this took a while - I was unable to install Ruby v3.2.0-preview3 locally due to this issue. However, fortunately that version is available in CircleCI builds.

The fix for this was released in v2.0.3.