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

sinon.match.each ? #1624

Closed
cyrilchapon opened this issue Nov 16, 2017 · 4 comments
Closed

sinon.match.each ? #1624

cyrilchapon opened this issue Nov 16, 2017 · 4 comments

Comments

@cyrilchapon
Copy link

It would be very great to have a matcher that exactly act like sinon.match.has but on every enumerated value in an array / object

Use case :

          ...
          const res = httpMocks.createResponse();
          res.json = sinon.spy(res.json);

          return stuffController.listStuff(req, res, next)
          .then(function() {
            assert.calledWith(res.json, sinon.match.array);
            assert.calledWith(res.json, sinon.match.has('length', expectedStuff.length));
            assert.calledWith(res.json, sinon.match(function(result) {
              return _.every(result, function(stuff) {
                return stuff instanceof Stuff;
              });
            }, 'Every return object is an stuff'));
          });

That could be turned to

          ...
          const res = httpMocks.createResponse();
          res.json = sinon.spy(res.json);

          return stuffController.listStuff(req, res, next)
          .then(function() {
            assert.calledWith(res.json, sinon.match.array);
            assert.calledWith(res.json, sinon.match.has('length', expectedStuff.length));
            assert.calledWith(res.json, sinon.match.each(result, sinon.match.instanceOf(Stuff)));
          });
@cyrilchapon
Copy link
Author

It could be named sinon.match.each, or lodash-like sinon.match.every

@cyrilchapon
Copy link
Author

In addition, one could find useful to check that at least one enumerated value matches.
We could have sinon.match.some for this

@mroderick
Copy link
Member

I think that would be a great addition to matchers.

My preference leans more towards using names that are more like Array.prototype.every and Array.prototype.some, than using lodash conventions. Keeping in mind just how inconsistent Sinon's API currently is, I don't think that preference matters that much.

nivsherf added a commit to nivsherf/sinon that referenced this issue Jan 16, 2018
Added new matcher - match.every, which matches on an object or
iterable if all of the properties (or elements, respectively) match
the given predicate.
nivsherf added a commit to nivsherf/sinon that referenced this issue Jan 16, 2018
Added new matcher - match.some, which matches on an object or
iterable if some of the properties (or elements, respectively) match
the given predicate.
mroderick pushed a commit that referenced this issue Jan 21, 2018
* Add match.every (#1624)

Added new matcher - match.every, which matches on an object or
iterable if all of the properties (or elements, respectively) match
the given predicate.

* Add match.some (#1624)

Added new matcher - match.some, which matches on an object or
iterable if some of the properties (or elements, respectively) match
the given predicate.
@mroderick
Copy link
Member

This has been implemented by #1661 and shipped as sinon@4.2.0

franck-romano pushed a commit to franck-romano/sinon that referenced this issue Oct 1, 2019
* Add match.every (sinonjs#1624)

Added new matcher - match.every, which matches on an object or
iterable if all of the properties (or elements, respectively) match
the given predicate.

* Add match.some (sinonjs#1624)

Added new matcher - match.some, which matches on an object or
iterable if some of the properties (or elements, respectively) match
the given predicate.
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

3 participants