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

Fix .with when multiple identical arguments #56

Merged
merged 2 commits into from Aug 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 21 additions & 27 deletions lib/spy.js
Expand Up @@ -264,46 +264,40 @@ module.exports = function (chai, _) {

function assertWith () {
new Assertion(this._obj).to.be.spy;
var args = [].slice.call(arguments, 0)
var expArgs = [].slice.call(arguments, 0)
, calls = this._obj.__spy.calls
, always = _.flag(this, 'spy always')
, passed;

if (always) {
passed = 0
calls.forEach(function (call) {
var found = 0;
args.forEach(function (arg) {
for (var i = 0; i < call.length; i++) {
if (_.eql(call[i], arg)) found++;
, passed = 0;

calls.forEach(function (call) {
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 === args.length) passed++;
}
});
if (found === expArgs.length) passed++;
});

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._his + ' to have not always been called with #{exp}'
, args
, 'expected ' + this._obj + ' to have not always been called with #{exp}'
, expArgs
);
} else {
passed = 0;
calls.forEach(function (call) {
var found = 0;
args.forEach(function (arg) {
for (var i = 0; i < call.length; i++) {
if (_.eql(call[i], arg)) found++;
}
});
if (found === args.length) passed++;
});

this.assert(
passed > 0
, 'expected ' + this._obj + ' to have been called with #{exp}'
, 'expected ' + this._his + ' to have not been called with #{exp} but got ' + passed + ' times'
, args
, 'expected ' + this._obj + ' to have not been called with #{exp} but got ' + passed + ' times'
, expArgs
);
}
}
Expand Down
38 changes: 38 additions & 0 deletions test/spies.js
Expand Up @@ -276,6 +276,15 @@ describe('Chai Spies', function () {
spy.should.have.not.been.called.with(3,6,9);
}).should.throw(chai.AssertionError, /have not been called with/);
});

it('should pass when called with multiple identical arguments', function () {
var spy = chai.spy();
spy(1, 1);
spy.should.have.been.called.with(1);
spy.should.have.been.called.with(1, 1);
spy.should.not.have.been.called.with(1, 2);
spy.should.not.have.been.called.with(1, 1, 1);
});
});

describe('.always.with(arg, ...)', function () {
Expand Down Expand Up @@ -312,6 +321,16 @@ describe('Chai Spies', function () {
spy.should.not.have.been.always.called.with(1,2);
}).should.throw(chai.AssertionError, /to have not always been called with/);
});

it('should pass when called with multiple identical arguments', function () {
var spy = chai.spy();
spy(1, 3, 1);
spy(1, 2, 1);
spy.should.have.always.been.called.with(1);
spy.should.have.always.been.called.with(1, 1);
spy.should.not.have.always.been.called.with(1, 2);
spy.should.not.have.always.been.called.with(1, 1, 1);
});
});

describe('.with.exactly(arg, ...)', function () {
Expand Down Expand Up @@ -342,6 +361,15 @@ describe('Chai Spies', function () {
spy.should.have.not.been.called.with.exactly(3,2);
}).should.throw(chai.AssertionError, /to not have been called with exactly/);
});

it('should pass when called with multiple identical arguments', function () {
var spy = chai.spy();
spy(1, 1);
spy.should.have.been.called.with.exactly(1, 1);
spy.should.not.have.been.called.with.exactly(1);
spy.should.not.have.been.called.with.exactly(1, 2);
spy.should.not.have.been.called.with.exactly(1, 1, 1);
});
});

describe('.always.with.exactly(arg, ...)', function () {
Expand Down Expand Up @@ -376,6 +404,16 @@ describe('Chai Spies', function () {
spy2.should.have.been.always.called.with.exactly(4,4);
}).should.throw(chai.AssertionError, /to have been always called with exactly/);
});

it('should pass when called with multiple identical arguments', function () {
var spy = chai.spy();
spy(1, 1);
spy(1, 1);
spy.should.have.always.been.called.with.exactly(1, 1);
spy.should.not.have.always.been.called.with.exactly(1);
spy.should.not.have.always.been.called.with.exactly(1, 2);
spy.should.not.have.always.been.called.with.exactly(1, 1, 1);
});
});

describe('spy object', function () {
Expand Down