You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I started getting some "Using the last argument as keyword parameters is deprecated" warnings from a test I was writing with Minitest::Mock. The method being mocked doesn't expect any keyword arguments, just a single Hash.
Here are a few tests demonstrating the code that elicits those warnings as well as a few other things I wouldn't expect (e.g., passing a copy of the hash). These pass in 5.15.0 with no warnings but fail in 5.16.x. I'm using Ruby 2.7.4.
require'bundler/inline'gemfiledosource'https://rubygems.org'#gem 'minitest', '=5.15.0'gem'minitest','=5.16.3'endrequire'minitest/autorun'require'minitest/mock'classMockExpectHashTest < Minitest::Testdeftest_hash_argument_with_blockexpected_options={one: 1}mock=Minitest::Mock.newmock.expect(:options,expected_options)do |options|
options == expected_optionsend# This line causes a warning with minitest >=5.16.0:# warning: Using the last argument as keyword parameters is# deprecated; maybe ** should be added to the callmock.options(expected_options)mock.verifyenddeftest_hash_argumentexpected_options={one: 1}mock=Minitest::Mock.newmock.expect(:options,expected_options,[expected_options])# This line causes a warning with minitest >=5.16.0:# warning: Using the last argument as keyword parameters is# deprecated; maybe ** should be added to the call# It also fails with:# ArgumentError: mocked method :options expects 1 arguments, got []mock.options(expected_options)mock.verifyenddeftest_hash_argument_with_block_and_equal?expected_options={one: 1}mock=Minitest::Mock.newmock.expect(:options,expected_options)do |options|
options.equal?expected_optionsend# Fails w/ minitest >=5.16.0mock.options(expected_options)mock.verifyenddeftest_frozen_hash?expected_options={one: 1}.freezemock=Minitest::Mock.newmock.expect(:options,expected_options)do |options|
options.frozen?end# Fails w/ minitest >=5.16.0mock.options(expected_options)mock.verifyend# edited to add this testdeftest_hash_with_default_procexpected_hash=Hash.new(2)mock=Minitest::Mock.newmock.expect(:options,expected_hash)do |options|
options[:foo] == 2end# Fails w/ minitest >=5.16.0mock.options(expected_hash)mock.verifyendend
It seems that the "kwargs" hack breaks passing hashes normally?
The text was updated successfully, but these errors were encountered:
As far as I can tell, this is caused by #method_missing's **kwargs parameter.
What about something like this, added to the Minitest::Mock class after the definition of method_missing?
ifRUBY_VERSION < '3.0'alias__method_missingmethod_missingdefmethod_missingsym, *args, &block# Let the original method handle this.return__method_missing(sym, *args)unless@expected_calls.key?(sym)index=@actual_calls[sym].lengthexpected_call=@expected_calls[sym][index]# Let the original method handle this, too.return__method_missing(sym, *args)unlessexpected_callexpected_kwargs=expected_call[:kwargs]val_block=expected_call[:block]# Is this call expecting keyword arguments?expecting_kwargs=expected_kwargs &&
(expected_kwargs == Hash || !expected_kwargs.empty?) ||
val_block &&
val_block.parameters.any?{ |type,_| type.to_s.start_with?'key'}# If not, or if none are present, use an empty Hash.kwargs=expecting_kwargs && Hash === args.last ? args.pop : {}__method_missing(sym, *args, **kwargs, &block)endend
I tested this on 4accdd5 with Ruby 2.5.1 and 2.7.4. My tests above pass, and only one of the test/minitest/test_minitest_mock.rb tests fails, test_mock_block_is_passed_keyword_args__args__old_style_bad. (That test is because of the current behavior of assuming that if the last arg is a hash, it is keyword args?)
I started getting some "Using the last argument as keyword parameters is deprecated" warnings from a test I was writing with Minitest::Mock. The method being mocked doesn't expect any keyword arguments, just a single Hash.
Here are a few tests demonstrating the code that elicits those warnings as well as a few other things I wouldn't expect (e.g., passing a copy of the hash). These pass in 5.15.0 with no warnings but fail in 5.16.x. I'm using Ruby 2.7.4.
It seems that the "kwargs" hack breaks passing hashes normally?
The text was updated successfully, but these errors were encountered: