Skip to content
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

Expectations with String keys misidentified as keyword arguments #594

Open
seandilda opened this issue Dec 13, 2022 · 4 comments
Open

Expectations with String keys misidentified as keyword arguments #594

seandilda opened this issue Dec 13, 2022 · 4 comments
Assignees

Comments

@seandilda
Copy link

I'm testing mocha 2.0.2 on ruby 3.1.3 and ran into an issue where a parameter matcher specifying a hash with String keys is being treated as keyword arguments by mocha.

Example code:

require 'mocha/api'

include Mocha::API
mocha_setup

class Example
  def foo(args); end
end

example = Example.new
example.expects(:foo).with('key' => 'value')
example.foo({ 'key' => 'value' })

This produces the following deprecation warning:

Mocha deprecation warning at mocha_test.rb:12:in `<main>': Expectation defined at mocha_test.rb:11:in `<main>' expected keyword arguments ("key" => "value"), but received positional hash ({"key" => "value"}). These will stop matching when strict keyword argument matching is enabled. See the documentation for Mocha::Configuration#strict_keyword_argument_matching=.

It looks like mocha treated .with('key' => 'value') as specifying a keyword argument instead of a Hash argument. Since keyword argument syntax looks like a hash with symbol keys, it should be safe to assume that any the parameter is intended to be a Hash if any of the keys aren't Symbols.

@floehopper floehopper self-assigned this Dec 13, 2022
@floehopper
Copy link
Member

@seandilda

Am I right in thinking this is just a special case of #593?

@seandilda
Copy link
Author

@floehopper they are definitely related. I see #593 as an request for enhancement, as I believe the code is likely working as intended, but maybe not the way that would be most useful for users like myself.

I see this issue as a bug as an expectation that is unambiguously a Hash expectation is being misidentified as being a keyword expectation.

@floehopper
Copy link
Member

@seandilda Thanks for explaining. While I think I see the distinction you are drawing from a user's point of view, in implementation terms I don't think there's going to be any meaningful difference between this and #593. I may have missed something, but if #593 is fixed then that should also fix this issue - is that correct?

@seandilda
Copy link
Author

There are some slight differences in the issues, but it is possible a general fix for #593 will also fix this issue.

This issue is about how the expected parameters are parsed. #593 is about how the arguments used when calling the mock are parsed/compared to expected parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants