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
Check on spy call order by parameter #59
Comments
Any new about this? I want to work for this feature. |
We store the calls as an array of arguments, so this seems like an entirely reasonable feature to add. I'd personally like to see this feature as well. In addition I'd like to see: const spy = chai.spy();
spy('a')
spy('b')
spy('c')
expect(spy).to.be.called.with('b').after.being.called.with('a') Perhaps that's a discussion for another time though. The change:Adding the flags Who can make this PR?Anyone who wants to contribute to this project is welcome to make this PR! I want to make this PR!Great! You should take a look at our Code of Conduct and Contribution Guidelines (note: we need to work on our contribution guidelines, so these might not apply exactly to this sub-project). If you want to work on this, you should comment below, something like:
If you don't comment you might run the risk of someone else doing the work before you! If someone has already commented, then you should leave this PR for them to work on. If someone has commented, but a few weeks have passed - it is worth asking the person who has commented if they're still interested in working on this, and if you can pick it up instead. How to make a PR for this change:You'll need to add the flags Assertion.addProperty('first', function () {
if ('undefined' !== this._obj.__spy) {
_.flag(this, 'spy call index', 1);
}
});
Assertion.addProperty('second', function () {
if ('undefined' !== this._obj.__spy) {
_.flag(this, 'spy call index', 2);
}
});
Assertion.addProperty('third', function () {
if ('undefined' !== this._obj.__spy) {
_.flag(this, 'spy call index', 3);
}
}); You'll then need to modify the function assertWith () {
new Assertion(this._obj).to.be.spy;
var expArgs = [].slice.call(arguments, 0)
, calls = this._obj.__spy.calls
, always = _.flag(this, 'spy always')
, expectedCallIndex = _.flag(this, 'spy call index')
, callIndex = null
, passed = 0;
calls.forEach(function (call, index) {
var actArgs = call.slice()
, found = 0;
expArgs.forEach(function (expArg) {
for (var i = 0; i < actArgs.length; i++) {
if (_.eql(actArgs[i], expArg)) {
found++;
actArgs.splice(i, 1);
break;
}
}
});
if (found === expArgs.length) {
passed++;
callIndex = callIndex === null ? index : callIndex;
}
});
if (typeof expectedCallIndex === 'number') {
var extraMessage = 'was not called'
if (callIndex >= 0) {
extraMessage = 'was called on call ' + callIndex
}
this.assert(
expectedCallIndex === callIndex
, 'expected ' + this._obj + ' to have been called with #{exp} on call ' + expectedCallIndex + ' but ' + extraMessage,
, 'expected ' + this._obj + ' to not have been called with #{exp} on call ' + expectedCallindex
, expArgs
)
} else if (always) {
this.assert(
passed === calls.length
, 'expected ' + this._obj + ' to have been always called with #{exp} but got ' + passed + ' out of ' + calls.length
, 'expected ' + this._obj + ' to have not always been called with #{exp}'
, expArgs
);
} else {
this.assert(
passed > 0
, 'expected ' + this._obj + ' to have been called with #{exp}'
, 'expected ' + this._obj + ' to have not been called with #{exp} but got ' + passed + ' times'
, expArgs
);
}
} As always, you'll need to write some tests to back this up and also add some docs in the README. If you want to implement an Once if you done this, if you run the What happens then?We'll need 2 contributors to accept the change, and it'll be merged. Once merged, we'll need to take some time to make a manual release, which will take a while - as we need to prepare release notes. It'll likely be released as part of a set of changes for a major chai-spies version. |
I want to make this pull request. I forked yesterday and I will keep working on this and come back to you with news. Regards. |
I commited first, second and third modifiers for the with method. It may make sense to also have nth modifier/method, what do you think? For the after modifier I have to plan first how to do that hehe On the other hand, which branch should I pick to create the pull request against this project? Regards. |
Great work @cnexans! We could add an For now - feel free to PR against |
Hey @keithamus thank you. Created the pr for the first four assertions: I guess we'd talk about Regards |
@keithamus how should it work in case of checking multiple args? spy(1,2,3)
expect(spy).to.first.be.called.with(2,3) // is this true or error? I'd expect error
expect(spy).to.first.be.called.with(1) // the same question, true or error? I'd expect true Alternatively there may be something like |
@stalniy we already have an |
closed by #75 |
In some cases is needed to check that the same spy has been called multiple times with different parameters (eg: while testing a curried function)
This is a possible desired feature, suggested by @keithamus:
It could be hard to match/generate
first
,second
,third
,nth
property in the assertion chain.Another possible assertion structure is:
The text was updated successfully, but these errors were encountered: