From fed2129e617793fa00cb49429fdd6d51e4ab1c81 Mon Sep 17 00:00:00 2001 From: David Mott Date: Wed, 28 Aug 2019 13:56:08 -0400 Subject: [PATCH] Proposed resolution to issue 2006 --- lib/sinon/assert.js | 1 + test/assert-test.js | 136 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 136 insertions(+), 1 deletion(-) diff --git a/lib/sinon/assert.js b/lib/sinon/assert.js index 3286e9aa1..d2eb83335 100644 --- a/lib/sinon/assert.js +++ b/lib/sinon/assert.js @@ -205,6 +205,7 @@ mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %D mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %D"); mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %D"); mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %D"); +mirrorPropAsAssertion("calledOnceWithExactly", "expected %n to be called once and with exact arguments %D"); mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %D"); mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C"); mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C"); diff --git a/test/assert-test.js b/test/assert-test.js index c73730e10..596e090eb 100644 --- a/test/assert-test.js +++ b/test/assert-test.js @@ -835,6 +835,104 @@ describe("assert", function() { }); }); + describe(".calledOnceWithExactly", function() { + requiresValidFake("calledOnceWithExactly"); + + it("fails when method fails", function() { + var object = {}; + sinonStub(this.stub, "calledOnceWithExactly").returns(false); + var stub = this.stub; + + assert.exception(function() { + sinonAssert.calledOnceWithExactly(stub, object, 1); + }); + + assert(this.stub.calledOnceWithExactly.calledOnceWithExactly(object, 1)); + assert(sinonAssert.fail.called); + }); + + it("passes when method doesn't fail", function() { + var object = {}; + sinonStub(this.stub, "calledOnceWithExactly").returns(true); + var stub = this.stub; + + refute.exception(function() { + sinonAssert.calledOnceWithExactly(stub, object, 1); + }); + + assert(this.stub.calledOnceWithExactly.calledOnceWithExactly(object, 1)); + assert.isFalse(sinonAssert.fail.called); + }); + + it("calls pass callback", function() { + this.stub("yeah"); + sinonAssert.calledOnceWithExactly(this.stub, "yeah"); + + assert(sinonAssert.pass.calledOnce); + assert(sinonAssert.pass.calledWith("calledOnceWithExactly")); + }); + + it("fails when method does not exist", function() { + assert.exception(function() { + sinonAssert.calledOnceWithExactly(); + }); + + assert(sinonAssert.fail.called); + }); + + it("fails when method is not stub", function() { + assert.exception(function() { + sinonAssert.calledOnceWithExactly(function() { + return; + }); + }); + + assert(sinonAssert.fail.called); + }); + + it("fails when method was not called", function() { + var stub = this.stub; + + assert.exception(function() { + sinonAssert.calledOnceWithExactly(stub); + }); + + assert(sinonAssert.fail.called); + }); + + it("fails when called with more than one argument", function() { + var stub = this.stub; + stub(); + + assert.exception(function() { + sinonAssert.calledOnceWithExactly(stub, 1); + }); + }); + + it("passes when method was called", function() { + var stub = this.stub; + stub(); + + refute.exception(function() { + sinonAssert.calledOnceWithExactly(stub); + }); + + assert.isFalse(sinonAssert.fail.called); + }); + + it("fails when method was called more than once", function() { + var stub = this.stub; + stub(); + stub(); + + assert.exception(function() { + sinonAssert.calledOnceWithExactly(stub); + }); + + assert(sinonAssert.fail.called); + }); + }); + describe(".neverCalledWith", function() { it("fails when method fails", function() { var object = {}; @@ -871,7 +969,7 @@ describe("assert", function() { }); }); - describe(".threwTest", function() { + describe(".threw", function() { it("fails when method fails", function() { sinonStub(this.stub, "threw").returns(false); var stub = this.stub; @@ -1113,6 +1211,7 @@ describe("assert", function() { assert.isFunction(test.assertCalledOn); assert.isFunction(test.assertCalledWith); assert.isFunction(test.assertCalledWithExactly); + assert.isFunction(test.assertCalledOnceWithExactly); assert.isFunction(test.assertThrew); assert.isFunction(test.assertCallCount); }); @@ -1128,6 +1227,7 @@ describe("assert", function() { assert.isFunction(assertCalledOn); assert.isFunction(assertCalledWith); assert.isFunction(assertCalledWithExactly); + assert.isFunction(assertCalledOnceWithExactly); assert.isFunction(assertThrew); assert.isFunction(assertCallCount); /*eslint-enable no-undef*/ @@ -1159,6 +1259,7 @@ describe("assert", function() { assert.isFunction(test.calledOn); assert.isFunction(test.calledWith); assert.isFunction(test.calledWithExactly); + assert.isFunction(test.calledOnceWithExactly); assert.isFunction(test.threw); assert.isFunction(test.callCount); }); @@ -1738,6 +1839,39 @@ describe("assert", function() { ); }); + it("assert.calledOnceWithExactly exception messages", function() { + assert.equals( + this.message("calledOnceWithExactly", this.obj.doSomething, 1, 3, "bob").replace(/ at.*/g, ""), + "expected doSomething to be called once and with exact arguments " + ); + + this.obj.doSomething(4, 3, "bob"); + assert.equals( + this.message("calledOnceWithExactly", this.obj.doSomething, 1, 3, "bob").replace(/ at.*/g, ""), + "expected doSomething to be called once and with exact arguments \n" + + color.red("4") + + " " + + color.green("1") + + " \n" + + "3\n" + + "bob" + ); + + this.obj.doSomething(); + assert.equals( + this.message("calledOnceWithExactly", this.obj.doSomething).replace(/ at.*/g, ""), + "expected doSomething to be called once and with exact arguments \n" + + "Call 1:\n" + + color.red("4") + + "\n" + + color.red("3") + + "\n" + + color.red("bob") + + "\n" + + "Call 2:" + ); + }); + it("assert.alwaysCalledWithExactly exception message", function() { this.obj.doSomething(1, 3, "hey"); this.obj.doSomething(1, 3);