New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object#stub fails with certain final arguments #918
Comments
I can't reproduce. This is what I currently have: #!/usr/bin/env ruby -w
require "minitest/mock"
class T
def self.create_with_retry(user, raw_params, limit: 3)
raise "shouldn't see this"
end
end
class C
def create current_user, params
T.create_with_retry(current_user, params)
end
end
class P
def inspect; "<params>"; end
def to_hash; { limit: 42 }; end
end
# this is the code run during all 3 stubs, it has a "P"arams as last arg
body = proc { C.new.create :user, P.new }
callable1 = ->(_user, _raw_params) { 42 }
callable2 = ->(_user, _raw_params, limit:false) { 42 } # had to add false
callable3 = ->(_user, _raw_params, **kwargs) { 42 }
T.stub(:create_with_retry, callable1, :user, P.new, &body)
T.stub(:create_with_retry, callable2, :user, P.new, &body)
T.stub(:create_with_retry, callable3, :user, P.new, &body)
# Passed: 2.6.10, 2.7.6, 3.0.4, 3.1.2 |
OK. Cluing in. I needed the to_hash to raise to emulate unpermitted params... This is a version with ALL the hacks in it needed to make it run. Still poking now that I better understand: #!/usr/bin/env ruby -w
require "minitest/mock"
class T
def self.create_with_retry(user, raw_params)
raise "shouldn't see this"
end
end
class C
def create current_user, params
T.create_with_retry current_user, params, **{}
end
end
class P
def inspect; "<params>"; end
def to_hash; raise "nah"; end
end
# this is the code run during all 3 stubs, it has a "P"arams as last arg
# NOTE: this has a forced kwargs kludge in it: **{} to prevent #to_hash
body = proc { C.new.create :user, P.new, **{} }
callable1 = ->(_user, _raw_params) { 42 }
callable2 = ->(_user, _raw_params, limit:false) { 42 } # had to add false
callable3 = ->(_user, _raw_params, **kwargs) { 42 }
p T.stub(:create_with_retry, callable1, &body)
p T.stub(:create_with_retry, callable2, &body)
p T.stub(:create_with_retry, callable3, &body) |
OK. I believe I have this fixed to work without the |
This is released in 5.16.2. Thanks for reporting! Please verify and close if this fixes your issues. |
We had a similar issue with mocked and stubbed tests when upgrading from Rails 5.2 to 6.1 using Ruby 2.7 This did fix our failures, thanks @zenspider 🥰 |
@tsugimoto does 5.16.2 and |
@blowmage I think this issue can get closed |
Confirmed with minitest 5.16.2 and |
From Minitest 5.16.0, the last argument of the stubbed method should not be certain kind of objects, unpermitted
ActionController::Parameters
in particular, because Ruby 2.7 tries to convert them into kwargs with#to_h
and raises.For example, the method
Auth::Token::create_or_retry
with this usage
couldn't be stubbed with any of these in Ruby 2.7 (some of them work in Ruby 3.0+)
Filed from #912 (comment)
The text was updated successfully, but these errors were encountered: