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

Add HasKeys parameter matcher #512

Closed
wants to merge 3 commits into from
Closed

Conversation

cstyles
Copy link
Contributor

@cstyles cstyles commented Jun 12, 2021

Much like how HasEntry has a related HasEntries matcher, this PR
adds a HasKeys matcher to accompany HasKey. With this new matcher,
instead of the following:

object.expects(:method).with(all_of(has_key(:key_1), has_key(:key_2), has_key(:key_3)))

We can just do this:

object.expects(:method).with(has_keys(:key_1, :key_2, :key_3))

Much like how `HasEntry` has a related `HasEntries` matcher, this commit
adds a `HasKeys` matcher to accompany `HasKey`. With this new matcher,
instead of the following:

```ruby
object.expects(:method).with(all_of(has_key(:key_1), has_key(:key_2), has_key(:key_3)))
```

We can just do this:

```ruby
object.expects(:method).with(has_keys(:key_1, :key_2, :key_3))
```
@floehopper
Copy link
Member

@cstyles Many thanks for this. It looks good to me. I'm having some issue with the Travis CI builds at the moment and I'm a bit short of time, so I might not get it merged for a few days. Please bear with me.

@floehopper
Copy link
Member

I've pushed up a slightly fixed-up version of this here: https://github.com/freerange/mocha/compare/has-keys

However, I'm not entirely happy with the implementation of HasKeys#mocha_inspect, i.e. slicing the output from Array#mocha_inspect using mocha_inspect[1...-1]. I'm going to give it some thought and see if I can come up with something I'm happier with. Sorry for the further delay in getting this merged.

@floehopper
Copy link
Member

Merged manually in d8f44bc & 18d8104. Closing. @cstyles Thanks again for contributing.

@floehopper floehopper closed this Jun 20, 2021
@floehopper
Copy link
Member

Released in v1.13.0.

See https://mocha.jamesmead.org/Mocha/ParameterMatchers.html#has_keys-instance_method.

@cstyles cstyles deleted the has-keys branch June 25, 2021 16:40
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 this pull request may close these issues.

None yet

2 participants