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

introduce #has_all parameter matcher #673

Open
mohammednasser-32 opened this issue Oct 5, 2024 · 3 comments
Open

introduce #has_all parameter matcher #673

mohammednasser-32 opened this issue Oct 5, 2024 · 3 comments

Comments

@mohammednasser-32
Copy link

Recently I was trying to mock a method that receives an array of ids as an argument, I don't care about the order of the ids so I am not sorting when fetching the records.
So the test was sometimes failing randomly as the order of the array is not consistent, example:

#test
my_service.stubs(:my_method).with([1,2])

#actual code on execution
my_service.my_method([2,1])

Using my_service.stubs(:my_method).with(includes(1, 2)) solved my issue...however the test will still pass if the array has an extra item which is not desirable in this case

my_service.my_method([3,2,1])

So I wanted to suggest introducing a has_all matcher that checks if two arrays contain the same elements regardless of their order.

If you think this would be useful, I would gladly open a PR for it

Thank you

@floehopper
Copy link
Member

@mohammednasser-32

Thanks for your interest in Mocha. I am open to the idea of adding a matcher like this, but I'm not convinced about the name has_all, because an array that contains more than just the listed items could be described as "having all" the items. I think I'd want to include the word "exact" in the name somehow; maybe includes_exactly...?

As an aside, if we add a matcher like this, I'd like to add a similar one for hashes as per the note for this commit:

... I've added an optional exact keyword argument to the HasEntries constructor and set this to true when it's called from PositionalOrKeywordHash#matches?. I haven't bothered to document the new exact argument, because currently it's only used internally.

I can imagine introducing something like #has_exact_entries to the public API, but it would probably make sense to add something similar for exact matching of array items at the same time, so I'm going to leave that for now.

@mohammednasser-32
Copy link
Author

mohammednasser-32 commented Oct 6, 2024

@floehopper perfect, I agree with includes_exactly..will work on it, and then I can work on has_exact_entries..thanks 🙏

@mohammednasser-32
Copy link
Author

@floehopper PR added

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

Successfully merging a pull request may close this issue.

2 participants