Skip to content

Commit 80fa9a5

Browse files
authored
Also mirror the calledOnceWith assertion (#2660)
1 parent 52b0d97 commit 80fa9a5

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

docs/release-source/release/assertions.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ Passes if `spy` was called with the provided arguments.
8787

8888
It's possible to assert on a dedicated spy call: `sinon.assert.calledWith(spy.firstCall, arg1, arg2, ...);`.
8989

90+
#### `sinon.assert.calledOnceWith(spyOrSpyCall, arg1, arg2, ...);`
91+
92+
Passes if `spy` was called once and only once with the provided arguments.
93+
94+
It's possible to assert on a dedicated spy call: `sinon.assert.calledOnceWith(spy.firstCall, arg1, arg2, ...);`.
95+
9096
#### `sinon.assert.alwaysCalledWith(spy, arg1, arg2, ...);`
9197

9298
Passes if `spy` was always called with the provided arguments.

lib/sinon/assert.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ function createAssertObject(opts) {
304304
"calledWithExactly",
305305
"expected %n to be called with exact arguments %D",
306306
);
307+
mirrorPropAsAssertion(
308+
"calledOnceWith",
309+
"expected %n to be called once and with arguments %D",
310+
);
307311
mirrorPropAsAssertion(
308312
"calledOnceWithExactly",
309313
"expected %n to be called once and with exact arguments %D",

test/assert-test.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,109 @@ describe("assert", function () {
841841
});
842842
});
843843

844+
describe(".calledOnceWith", function () {
845+
// eslint-disable-next-line mocha/no-setup-in-describe
846+
requiresValidFake("calledOnceWith");
847+
848+
it("fails when method fails", function () {
849+
const object = {};
850+
sinonStub(this.stub, "calledOnceWith").returns(false);
851+
const stub = this.stub;
852+
853+
assert.exception(function () {
854+
sinonAssert.calledOnceWith(stub, object, 1);
855+
});
856+
857+
assert(
858+
this.stub.calledOnceWith.calledOnceWithExactly(object, 1),
859+
);
860+
assert(sinonAssert.fail.called);
861+
});
862+
863+
it("passes when method doesn't fail", function () {
864+
const object = {};
865+
sinonStub(this.stub, "calledOnceWith").returns(true);
866+
const stub = this.stub;
867+
868+
refute.exception(function () {
869+
sinonAssert.calledOnceWith(stub, object, 1);
870+
});
871+
872+
assert(
873+
this.stub.calledOnceWith.calledOnceWithExactly(object, 1),
874+
);
875+
assert.isFalse(sinonAssert.fail.called);
876+
});
877+
878+
it("calls pass callback", function () {
879+
this.stub("yeah");
880+
sinonAssert.calledOnceWith(this.stub, "yeah");
881+
882+
assert(sinonAssert.pass.calledOnce);
883+
assert(sinonAssert.pass.calledWith("calledOnceWith"));
884+
});
885+
886+
it("fails when method does not exist", function () {
887+
assert.exception(function () {
888+
sinonAssert.calledOnceWith();
889+
});
890+
891+
assert(sinonAssert.fail.called);
892+
});
893+
894+
it("fails when method is not stub", function () {
895+
assert.exception(function () {
896+
sinonAssert.calledOnceWith(function () {
897+
return;
898+
});
899+
});
900+
901+
assert(sinonAssert.fail.called);
902+
});
903+
904+
it("fails when method was not called", function () {
905+
const stub = this.stub;
906+
907+
assert.exception(function () {
908+
sinonAssert.calledOnceWith(stub);
909+
});
910+
911+
assert(sinonAssert.fail.called);
912+
});
913+
914+
it("fails when called with more than one argument", function () {
915+
const stub = this.stub;
916+
stub();
917+
918+
assert.exception(function () {
919+
sinonAssert.calledOnceWith(stub, 1);
920+
});
921+
});
922+
923+
it("passes when method was called", function () {
924+
const stub = this.stub;
925+
stub();
926+
927+
refute.exception(function () {
928+
sinonAssert.calledOnceWith(stub);
929+
});
930+
931+
assert.isFalse(sinonAssert.fail.called);
932+
});
933+
934+
it("fails when method was called more than once", function () {
935+
const stub = this.stub;
936+
stub();
937+
stub();
938+
939+
assert.exception(function () {
940+
sinonAssert.calledOnceWith(stub);
941+
});
942+
943+
assert(sinonAssert.fail.called);
944+
});
945+
});
946+
844947
describe(".calledOnceWithExactly", function () {
845948
// eslint-disable-next-line mocha/no-setup-in-describe
846949
requiresValidFake("calledOnceWithExactly");

0 commit comments

Comments
 (0)