From 9766eb4a833c26c64012230b96dd1157ebb8e8a2 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Wed, 15 Jun 2022 12:44:11 -0400 Subject: [PATCH] Fix tests for minitest 5.16 In minitest/minitest@6e06ac9 minitest changed such that it now accepts `kwargs` instead of requiring kwargs to be shoved into the args array. This is a good change but required some updates to our test code to get the new version of minitest passing. Changes are as follows: 1) Lock minitest to 5.15 for Ruby 2.7. We don't love this change but it's pretty difficult to get 2.7 and 3.0 to play nicely together with the new kwargs changes. Dropping 2.7 support isn't an option right now for Rails. This is safe because all of the code changes here are internal methods to Rails like assert_called_with. Applications shouldn't be consuming them as they are no-doc'd. 2) Update the `assert_called_with` method to take any kwargs but also the returns kwarg. 3) Update callers of `assert_called_with` to move the kwargs outside the args array. 4) Update the message from marshaled exceptions. In 5.16 the exception message is "result not reported" instead of "Wrapped undumpable exception". Co-authored-by: Matthew Draper --- .../testing/method_call_assertions.rb | 22 +++- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/testing/method_call_assertions.rb b/activesupport/lib/active_support/testing/method_call_assertions.rb index 72451faaa8cc4..f146eefce0354 100644 --- a/activesupport/lib/active_support/testing/method_call_assertions.rb +++ b/activesupport/lib/active_support/testing/method_call_assertions.rb @@ -17,9 +17,9 @@ def assert_called(object, method_name, message = nil, times: 1, returns: nil, &b assert_equal times, times_called, error end - def assert_called_with(object, method_name, args, returns: nil, &block) + def assert_called_with(object, method_name, args, returns: false, **kwargs, &block) mock = Minitest::Mock.new - mock.expect(:call, returns, args) + expect_called_with(mock, args, returns: returns, **kwargs) object.stub(method_name, mock, &block) @@ -30,6 +30,24 @@ def assert_not_called(object, method_name, message = nil, &block) assert_called(object, method_name, message, times: 0, &block) end + #-- + # This method is a temporary wrapper for mock.expect as part of + # the Minitest 5.16 / Ruby 3.0 kwargs transition. It can go away + # when we drop support for Ruby 2.7. + if Minitest::Mock.instance_method(:expect).parameters.map(&:first).include?(:keyrest) + def expect_called_with(mock, args, returns: false, **kwargs) + mock.expect(:call, returns, args, **kwargs) + end + else + def expect_called_with(mock, args, returns: false, **kwargs) + if !kwargs.empty? + mock.expect(:call, returns, [*args, kwargs]) + else + mock.expect(:call, returns, args) + end + end + end + def assert_called_on_instance_of(klass, method_name, message = nil, times: 1, returns: nil) times_called = 0 klass.define_method("stubbed_#{method_name}") do |*|