Skip to content

Commit

Permalink
to have calls|a call|no calls: Support passing an array of stub insta…
Browse files Browse the repository at this point in the history
…nces or an array of sandboxes as the subject.
  • Loading branch information
papandreou committed Feb 1, 2017
1 parent 97fa4f0 commit 9fca5e9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/unexpected-sinon.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,16 +443,20 @@
return spies;
}

function extractSpies(spyOrArrayOrSinonSandbox) {
if (expect.findTypeOf(spyOrArrayOrSinonSandbox).is('sinonSandbox')) {
return spyOrArrayOrSinonSandbox.fakes || [];
} else if (Array.isArray(spyOrArrayOrSinonSandbox)) {
return spyOrArrayOrSinonSandbox;
} else if (spyOrArrayOrSinonSandbox && typeof spyOrArrayOrSinonSandbox === 'object') {
return extractSpiesFromSinonStubInstance(spyOrArrayOrSinonSandbox, expect);
function extractSpies(obj) {
if (expect.findTypeOf(obj).is('sinonSandbox')) {
return obj.fakes || [];
} else if (Array.isArray(obj)) {
var spies = [];
obj.forEach(function (item) {
Array.prototype.push.apply(spies, extractSpies(item));
});
return spies;
} else if (obj && typeof obj === 'object') {
return extractSpiesFromSinonStubInstance(obj, expect);
} else {
// Assume spy
return [ spyOrArrayOrSinonSandbox ];
return [ obj ];
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/unexpected-sinon.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,35 @@ describe('unexpected-sinon', function () {
});
});

describe('when passed an array of sinon stub instances as the subject', function () {
it('should succeed', function () {
var stubInstance1 = sinon.createStubInstance(MyClass);
stubInstance1.foo(123);
var stubInstance2 = sinon.createStubInstance(MyClass);
stubInstance2.foo(123);
return expect([stubInstance1, stubInstance2], 'to have no calls satisfying', function () {
stubInstance1.foo(456);
});
});

it('should fail with a diff', function () {
var stubInstance1 = sinon.createStubInstance(MyClass);
stubInstance1.foo(123);
var stubInstance2 = sinon.createStubInstance(MyClass);
stubInstance2.foo(123);
return expect(function () {
return expect([stubInstance1, stubInstance2], 'to have no calls satisfying', function () {
stubInstance1.foo(123);
});
}, 'to error with',
"expected [ MyClass({ foo: foo, bar: bar }), MyClass({ foo: foo, bar: bar }) ] to have no calls satisfying foo( 123 );\n" +
"\n" +
"foo( 123 ); at theFunction (theFileName:xx:yy)\n" +
"foo( 123 ); at theFunction (theFileName:xx:yy) // should be removed"
);
});
});

describe('when passed a spec object', function () {
it('should succeed when no spy call satisfies the spec', function () {
spy(123, 456);
Expand Down

0 comments on commit 9fca5e9

Please sign in to comment.