Argument error due to ruby 3.0 deprecation

Viewed 76

I recently upgraded the ruby version of my app to 3.0. After which multiple test cases were failing but all of them were of the same kind. For ex, post :create, :params => { :identifiedBy => { :label => epc }, :model => { :productId => productId } } or post :create, :params => create_params_kc_with_tid

All of the failing test cases gave the same error

ArgumentError: wrong number of arguments (given 2, expected 1)

I am not so sure about the root cause but I am suspecting it is happening due to the recent deprecation in ruby 3.0 i.e., Using the last argument as keyword parameters is deprecated

I am not so sure how to fix it. Please help me with the same.

1 Answers

:params => {} is a Hash of {params: {}} to use kwargs (keyword arguments) the syntax is similar but distinctly different to the parser.

To resolve this (and clean up your code a bit) change all instances of :symbol => value to be symbol: value.

For Example:

post :create, :params => { 
                 :identifiedBy => { :label => epc }, 
                 :model => { :productId => productId } }

becomes

post :create, params: { 
                 identifiedBy: { label: epc }, 
                 model:  { productId: productId } }

Now params will be treated as a keyword argument, additionally I would change all the old Hash rocket usages where the key is a symbol (as I did in my recommended change)

Related