Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 6 commits
  • 4 files changed
  • 0 comments
  • 2 contributors
2  lib/sinon/assert.js
@@ -155,6 +155,8 @@
155 155 mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
156 156 mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
157 157 mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t");
  158 + mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
  159 + mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
158 160 mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %*%C");
159 161 mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %*%C");
160 162 mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %*%C");
5 lib/sinon/spy.js
@@ -120,6 +120,11 @@
120 120 this.thisValues = [];
121 121 this.exceptions = [];
122 122 this.callIds = [];
  123 + if (this.fakes) {
  124 + for (var i = 0; i < this.fakes.length; i++) {
  125 + this.fakes[i].reset();
  126 + }
  127 + }
123 128 },
124 129
125 130 create: function create(func) {
125 test/sinon/assert_test.js
@@ -447,6 +447,116 @@ buster.testCase("sinon.assert", {
447 447 }
448 448 },
449 449
  450 + "calledWithNew": {
  451 + setUp: function () { this.setUpStubs(); },
  452 + tearDown: function () { this.tearDownStubs(); },
  453 +
  454 + "fails when method does not exist": function () {
  455 + sinon.stub(this.stub, "calledWithNew");
  456 +
  457 + assert.exception(function () {
  458 + sinon.assert.calledWithNew(null);
  459 + });
  460 +
  461 + assert.isFalse(this.stub.calledWithNew.called);
  462 + assert(sinon.assert.fail.called);
  463 + },
  464 +
  465 + "fails when method is not stub": function () {
  466 + sinon.stub(this.stub, "calledWithNew");
  467 +
  468 + assert.exception(function () {
  469 + sinon.assert.calledWithNew(function () {});
  470 + });
  471 +
  472 + assert.isFalse(this.stub.calledWithNew.called);
  473 + assert(sinon.assert.fail.called);
  474 + },
  475 +
  476 + "fails when method fails": function () {
  477 + sinon.stub(this.stub, "calledWithNew").returns(false);
  478 + var stub = this.stub;
  479 +
  480 + assert.exception(function () {
  481 + sinon.assert.calledWithNew(stub);
  482 + });
  483 +
  484 + assert(sinon.assert.fail.called);
  485 + },
  486 +
  487 + "passes when method doesn't fail": function () {
  488 + sinon.stub(this.stub, "calledWithNew").returns(true);
  489 + var stub = this.stub;
  490 +
  491 + sinon.assert.calledWithNew(stub);
  492 +
  493 + assert.isFalse(sinon.assert.fail.called);
  494 + },
  495 +
  496 + "calls pass callback": function () {
  497 + var a = new this.stub();
  498 + sinon.assert.calledWithNew(this.stub);
  499 +
  500 + assert(sinon.assert.pass.calledOnce);
  501 + assert(sinon.assert.pass.calledWith("calledWithNew"));
  502 + }
  503 + },
  504 +
  505 + "alwaysCalledWithNew": {
  506 + setUp: function () { this.setUpStubs(); },
  507 + tearDown: function () { this.tearDownStubs(); },
  508 +
  509 + "fails when method does not exist": function () {
  510 + sinon.stub(this.stub, "alwaysCalledWithNew");
  511 +
  512 + assert.exception(function () {
  513 + sinon.assert.alwaysCalledWithNew(null);
  514 + });
  515 +
  516 + assert.isFalse(this.stub.alwaysCalledWithNew.called);
  517 + assert(sinon.assert.fail.called);
  518 + },
  519 +
  520 + "fails when method is not stub": function () {
  521 + sinon.stub(this.stub, "alwaysCalledWithNew");
  522 +
  523 + assert.exception(function () {
  524 + sinon.assert.alwaysCalledWithNew(function () {});
  525 + });
  526 +
  527 + assert.isFalse(this.stub.alwaysCalledWithNew.called);
  528 + assert(sinon.assert.fail.called);
  529 + },
  530 +
  531 + "fails when method fails": function () {
  532 + sinon.stub(this.stub, "alwaysCalledWithNew").returns(false);
  533 + var stub = this.stub;
  534 +
  535 + assert.exception(function () {
  536 + sinon.assert.alwaysCalledWithNew(stub);
  537 + });
  538 +
  539 + assert(sinon.assert.fail.called);
  540 + },
  541 +
  542 + "passes when method doesn't fail": function () {
  543 + sinon.stub(this.stub, "alwaysCalledWithNew").returns(true);
  544 + var stub = this.stub;
  545 +
  546 + sinon.assert.alwaysCalledWithNew(stub);
  547 +
  548 + assert.isFalse(sinon.assert.fail.called);
  549 + },
  550 +
  551 + "calls pass callback": function () {
  552 + var a = new this.stub();
  553 + sinon.assert.alwaysCalledWithNew(this.stub);
  554 +
  555 + assert(sinon.assert.pass.calledOnce);
  556 + assert(sinon.assert.pass.calledWith("alwaysCalledWithNew"));
  557 + }
  558 + },
  559 +
450 560 "calledWith": {
451 561 setUp: function () { this.setUpStubs(); },
452 562 tearDown: function () { this.tearDownStubs(); },
@@ -987,6 +1097,21 @@ buster.testCase("sinon.assert", {
987 1097 "expected doSomething to always be called with [Oh yeah] as this but was called with [Oh no], [Oh well], [Oh yeah]");
988 1098 },
989 1099
  1100 + "assert.calledWithNew exception message": function () {
  1101 + this.obj.doSomething();
  1102 +
  1103 + assert.equals(this.message("calledWithNew", this.obj.doSomething),
  1104 + "expected doSomething to be called with new");
  1105 + },
  1106 +
  1107 + "assert.alwaysCalledWithNew exception message": function () {
  1108 + var a = new this.obj.doSomething();
  1109 + this.obj.doSomething();
  1110 +
  1111 + assert.equals(this.message("alwaysCalledWithNew", this.obj.doSomething),
  1112 + "expected doSomething to always be called with new");
  1113 + },
  1114 +
990 1115 "assert.calledWith exception message": function () {
991 1116 this.obj.doSomething(1, 3, "hey");
992 1117
52 test/sinon/spy_test.js
@@ -2282,13 +2282,8 @@ if (typeof require === "function" && typeof module === "object") {
2282 2282 }
2283 2283 },
2284 2284
2285   - "reset": {
2286   - "resets spy state": function () {
2287   - var spy = sinon.spy();
2288   - spy();
2289   -
2290   - spy.reset();
2291   -
  2285 + "reset": (function () {
  2286 + function assertReset(spy) {
2292 2287 assert(!spy.called);
2293 2288 assert(!spy.calledOnce);
2294 2289 assert.equals(spy.args.length, 0);
@@ -2299,18 +2294,45 @@ if (typeof require === "function" && typeof module === "object") {
2299 2294 assert.isNull(spy.secondCall);
2300 2295 assert.isNull(spy.thirdCall);
2301 2296 assert.isNull(spy.lastCall);
2302   - },
  2297 + }
  2298 +
  2299 + return {
  2300 + "resets spy state": function () {
  2301 + var spy = sinon.spy();
  2302 + spy();
2303 2303
2304   - "resets call order state": function () {
2305   - var spies = [sinon.spy(), sinon.spy()];
2306   - spies[0]();
2307   - spies[1]();
  2304 + spy.reset();
2308 2305
2309   - spies[0].reset();
  2306 + assertReset(spy);
  2307 + },
2310 2308
2311   - assert(!spies[0].calledBefore(spies[1]));
  2309 + "resets call order state": function () {
  2310 + var spies = [sinon.spy(), sinon.spy()];
  2311 + spies[0]();
  2312 + spies[1]();
  2313 +
  2314 + spies[0].reset();
  2315 +
  2316 + assert(!spies[0].calledBefore(spies[1]));
  2317 + },
  2318 +
  2319 + "resets fakes returned by withArgs": function () {
  2320 + var spy = sinon.spy();
  2321 + var fakeA = spy.withArgs("a");
  2322 + var fakeB = spy.withArgs("b");
  2323 + spy("a");
  2324 + spy("b");
  2325 + spy("c");
  2326 + var fakeC = spy.withArgs("c");
  2327 +
  2328 + spy.reset();
  2329 +
  2330 + assertReset(fakeA);
  2331 + assertReset(fakeB);
  2332 + assertReset(fakeC);
  2333 + }
2312 2334 }
2313   - },
  2335 + }()),
2314 2336
2315 2337 "withArgs": {
2316 2338 "defines withArgs method": function () {

No commit comments for this range

Something went wrong with that request. Please try again.