How to use requiresValidFake method in sinon

Best JavaScript code snippet using sinon

assert-test.js

Source:assert-test.js Github

copy

Full Screen

...6var sinonAssert = require("../lib/sinon/assert");7var match = require("@sinonjs/samsam").createMatcher;8var assert = referee.assert;9var refute = referee.refute;10function requiresValidFake(method) {11 it("should fail with non-function fake", function() {12 assert.exception(function() {13 sinonAssert[method]({});14 });15 });16}17describe("assert", function() {18 beforeEach(function() {19 this.global = typeof window !== "undefined" ? window : global;20 this.setUpStubs = function() {21 this.stub = sinonStub();22 sinonStub(sinonAssert, "fail").throws();23 sinonStub(sinonAssert, "pass");24 };25 this.tearDownStubs = function() {26 sinonAssert.fail.restore();27 sinonAssert.pass.restore();28 };29 });30 it("is object", function() {31 assert.isObject(sinonAssert);32 });33 it("supports proxy property", function() {34 var api = {35 method: function() {36 return;37 }38 };39 api.method.proxy = function() {40 return;41 };42 sinonSpy(api, "method");43 api.method();44 refute.exception(function() {45 sinonAssert.calledOnce(api.method);46 });47 });48 describe(".fail", function() {49 beforeEach(function() {50 this.exceptionName = sinonAssert.failException;51 });52 afterEach(function() {53 sinonAssert.failException = this.exceptionName;54 });55 it("throws exception", function() {56 assert.exception(57 function() {58 sinonAssert.fail("Some message");59 },60 {61 name: "AssertError"62 }63 );64 });65 it("throws configured exception type", function() {66 sinonAssert.failException = "CustomError";67 assert.exception(68 function() {69 sinonAssert.fail("Some message");70 },71 { name: "CustomError" }72 );73 });74 });75 describe("with stubs", function() {76 beforeEach(function() {77 this.setUpStubs();78 });79 afterEach(function() {80 this.tearDownStubs();81 });82 describe(".match", function() {83 it("fails when arguments to not match", function() {84 assert.exception(function() {85 sinonAssert.match("foo", "bar");86 });87 assert(sinonAssert.fail.calledOnce);88 });89 it("passes when argumens match", function() {90 sinonAssert.match("foo", "foo");91 assert(sinonAssert.pass.calledOnce);92 });93 });94 describe(".called", function() {95 // eslint-disable-next-line mocha/no-setup-in-describe96 requiresValidFake("called");97 it("fails when method does not exist", function() {98 assert.exception(function() {99 sinonAssert.called();100 });101 assert(sinonAssert.fail.called);102 });103 it("fails when method is not stub", function() {104 assert.exception(function() {105 sinonAssert.called(function() {106 return;107 });108 });109 assert(sinonAssert.fail.called);110 });111 it("fails when method was not called", function() {112 var stub = this.stub;113 assert.exception(function() {114 sinonAssert.called(stub);115 });116 assert(sinonAssert.fail.called);117 });118 it("fails when called with more than one argument", function() {119 var stub = this.stub;120 stub();121 assert.exception(function() {122 sinonAssert.called(stub, 1);123 });124 });125 it("does not fail when method was called", function() {126 var stub = this.stub;127 stub();128 refute.exception(function() {129 sinonAssert.called(stub);130 });131 assert.isFalse(sinonAssert.fail.called);132 });133 it("calls pass callback", function() {134 var stub = this.stub;135 stub();136 refute.exception(function() {137 sinonAssert.called(stub);138 });139 assert(sinonAssert.pass.calledOnce);140 assert(sinonAssert.pass.calledWith("called"));141 });142 });143 describe(".notCalled", function() {144 // eslint-disable-next-line mocha/no-setup-in-describe145 requiresValidFake("notCalled");146 it("fails when method does not exist", function() {147 assert.exception(function() {148 sinonAssert.notCalled();149 });150 assert(sinonAssert.fail.called);151 });152 it("fails when method is not stub", function() {153 assert.exception(function() {154 sinonAssert.notCalled(function() {155 return;156 });157 });158 assert(sinonAssert.fail.called);159 });160 it("fails when method was called", function() {161 var stub = this.stub;162 stub();163 assert.exception(function() {164 sinonAssert.notCalled(stub);165 });166 assert(sinonAssert.fail.called);167 });168 it("fails when called with more than one argument", function() {169 var stub = this.stub;170 assert.exception(function() {171 sinonAssert.notCalled(stub, 1);172 });173 });174 it("passes when method was not called", function() {175 var stub = this.stub;176 refute.exception(function() {177 sinonAssert.notCalled(stub);178 });179 assert.isFalse(sinonAssert.fail.called);180 });181 it("should call pass callback", function() {182 var stub = this.stub;183 sinonAssert.notCalled(stub);184 assert(sinonAssert.pass.calledOnce);185 assert(sinonAssert.pass.calledWith("notCalled"));186 });187 });188 describe(".calledOnce", function() {189 // eslint-disable-next-line mocha/no-setup-in-describe190 requiresValidFake("calledOnce");191 it("fails when method does not exist", function() {192 assert.exception(function() {193 sinonAssert.calledOnce();194 });195 assert(sinonAssert.fail.called);196 });197 it("fails when method is not stub", function() {198 assert.exception(function() {199 sinonAssert.calledOnce(function() {200 return;201 });202 });203 assert(sinonAssert.fail.called);204 });205 it("fails when method was not called", function() {206 var stub = this.stub;207 assert.exception(function() {208 sinonAssert.calledOnce(stub);209 });210 assert(sinonAssert.fail.called);211 });212 it("fails when called with more than one argument", function() {213 var stub = this.stub;214 stub();215 assert.exception(function() {216 sinonAssert.calledOnce(stub, 1);217 });218 });219 it("passes when method was called", function() {220 var stub = this.stub;221 stub();222 refute.exception(function() {223 sinonAssert.calledOnce(stub);224 });225 assert.isFalse(sinonAssert.fail.called);226 });227 it("fails when method was called more than once", function() {228 var stub = this.stub;229 stub();230 stub();231 assert.exception(function() {232 sinonAssert.calledOnce(stub);233 });234 assert(sinonAssert.fail.called);235 });236 it("calls pass callback", function() {237 var stub = this.stub;238 stub();239 sinonAssert.calledOnce(stub);240 assert(sinonAssert.pass.calledOnce);241 assert(sinonAssert.pass.calledWith("calledOnce"));242 });243 });244 describe(".calledTwice", function() {245 // eslint-disable-next-line mocha/no-setup-in-describe246 requiresValidFake("calledTwice");247 it("fails if called once", function() {248 var stub = this.stub;249 this.stub();250 assert.exception(function() {251 sinonAssert.calledTwice(stub);252 });253 });254 it("fails when called with more than one argument", function() {255 var stub = this.stub;256 this.stub();257 this.stub();258 assert.exception(function() {259 sinonAssert.calledTwice(stub, 1);260 });261 });262 it("passes if called twice", function() {263 var stub = this.stub;264 this.stub();265 this.stub();266 refute.exception(function() {267 sinonAssert.calledTwice(stub);268 });269 });270 it("calls pass callback", function() {271 var stub = this.stub;272 stub();273 stub();274 sinonAssert.calledTwice(stub);275 assert(sinonAssert.pass.calledOnce);276 assert(sinonAssert.pass.calledWith("calledTwice"));277 });278 });279 describe(".calledThrice", function() {280 // eslint-disable-next-line mocha/no-setup-in-describe281 requiresValidFake("calledThrice");282 it("fails if called once", function() {283 var stub = this.stub;284 this.stub();285 assert.exception(function() {286 sinonAssert.calledThrice(stub);287 });288 });289 it("fails when called with more than one argument", function() {290 var stub = this.stub;291 this.stub();292 this.stub();293 this.stub();294 assert.exception(function() {295 sinonAssert.calledThrice(stub, 1);296 });297 });298 it("passes if called thrice", function() {299 var stub = this.stub;300 this.stub();301 this.stub();302 this.stub();303 refute.exception(function() {304 sinonAssert.calledThrice(stub);305 });306 });307 it("calls pass callback", function() {308 var stub = this.stub;309 stub();310 stub();311 stub();312 sinonAssert.calledThrice(stub);313 assert(sinonAssert.pass.calledOnce);314 assert(sinonAssert.pass.calledWith("calledThrice"));315 });316 });317 describe(".callOrder", function() {318 it("passes when calls were done in right order", function() {319 var spy1 = sinonSpy();320 var spy2 = sinonSpy();321 spy1();322 spy2();323 refute.exception(function() {324 sinonAssert.callOrder(spy1, spy2);325 });326 });327 it("fails when calls were done in wrong order", function() {328 var spy1 = sinonSpy();329 var spy2 = sinonSpy();330 spy2();331 spy1();332 assert.exception(function() {333 sinonAssert.callOrder(spy1, spy2);334 });335 assert(sinonAssert.fail.called);336 });337 it("passes when many calls were done in right order", function() {338 var spy1 = sinonSpy();339 var spy2 = sinonSpy();340 var spy3 = sinonSpy();341 var spy4 = sinonSpy();342 spy1();343 spy2();344 spy3();345 spy4();346 refute.exception(function() {347 sinonAssert.callOrder(spy1, spy2, spy3, spy4);348 });349 });350 it("fails when one of many calls were done in wrong order", function() {351 var spy1 = sinonSpy();352 var spy2 = sinonSpy();353 var spy3 = sinonSpy();354 var spy4 = sinonSpy();355 spy1();356 spy2();357 spy4();358 spy3();359 assert.exception(function() {360 sinonAssert.callOrder(spy1, spy2, spy3, spy4);361 });362 assert(sinonAssert.fail.called);363 });364 it("calls pass callback", function() {365 var stubs = [sinonSpy(), sinonSpy()];366 stubs[0]();367 stubs[1]();368 sinonAssert.callOrder(stubs[0], stubs[1]);369 assert(sinonAssert.pass.calledOnce);370 assert(sinonAssert.pass.calledWith("callOrder"));371 });372 it("passes for multiple calls to same spy", function() {373 var first = sinonSpy();374 var second = sinonSpy();375 first();376 second();377 first();378 refute.exception(function() {379 sinonAssert.callOrder(first, second, first);380 });381 });382 it("fails if first spy was not called", function() {383 var first = sinonSpy();384 var second = sinonSpy();385 second();386 assert.exception(function() {387 sinonAssert.callOrder(first, second);388 });389 });390 it("fails if second spy was not called", function() {391 var first = sinonSpy();392 var second = sinonSpy();393 first();394 assert.exception(function() {395 sinonAssert.callOrder(first, second);396 });397 });398 });399 describe(".calledOn", function() {400 it("fails when method does not exist", function() {401 var object = {};402 sinonStub(this.stub, "calledOn");403 assert.exception(function() {404 sinonAssert.calledOn(null, object);405 });406 assert.isFalse(this.stub.calledOn.calledWith(object));407 assert(sinonAssert.fail.called);408 });409 it("fails when method is not stub", function() {410 var object = {};411 sinonStub(this.stub, "calledOn");412 assert.exception(function() {413 sinonAssert.calledOn(function() {414 return;415 }, object);416 });417 assert.isFalse(this.stub.calledOn.calledWith(object));418 assert(sinonAssert.fail.called);419 });420 it("fails when method fails", function() {421 var object = {};422 sinonStub(this.stub, "calledOn").returns(false);423 var stub = this.stub;424 assert.exception(function() {425 sinonAssert.calledOn(stub, object);426 });427 assert(sinonAssert.fail.called);428 });429 it("passes when method doesn't fail", function() {430 var object = {};431 sinonStub(this.stub, "calledOn").returns(true);432 var stub = this.stub;433 sinonAssert.calledOn(stub, object);434 assert.isFalse(sinonAssert.fail.called);435 });436 it("calls pass callback", function() {437 var obj = {};438 this.stub.call(obj);439 sinonAssert.calledOn(this.stub, obj);440 assert(sinonAssert.pass.calledOnce);441 assert(sinonAssert.pass.calledWith("calledOn"));442 });443 it("works with spyCall", function() {444 var spy = sinonSpy();445 var target = {};446 spy();447 spy.call(target);448 sinonAssert.calledOn(spy.lastCall, target);449 assert(sinonAssert.pass.calledOn);450 assert(sinonAssert.pass.calledWith("calledOn"));451 });452 it("fails when spyCall failed", function() {453 var spy = sinonSpy();454 var target = {};455 spy();456 spy.call(target);457 assert.exception(function() {458 sinonAssert.calledOn(spy.lastCall, 1);459 });460 assert(sinonAssert.fail.called);461 });462 });463 describe(".calledWithNew", function() {464 // eslint-disable-next-line mocha/no-setup-in-describe465 requiresValidFake("calledWithNew");466 it("fails when method does not exist", function() {467 sinonStub(this.stub, "calledWithNew");468 assert.exception(function() {469 sinonAssert.calledWithNew(null);470 });471 assert.isFalse(this.stub.calledWithNew.called);472 assert(sinonAssert.fail.called);473 });474 it("fails when method is not stub", function() {475 sinonStub(this.stub, "calledWithNew");476 assert.exception(function() {477 sinonAssert.calledWithNew(function() {478 return;479 });480 });481 assert.isFalse(this.stub.calledWithNew.called);482 assert(sinonAssert.fail.called);483 });484 it("fails when method fails", function() {485 sinonStub(this.stub, "calledWithNew").returns(false);486 var stub = this.stub;487 assert.exception(function() {488 sinonAssert.calledWithNew(stub);489 });490 assert(sinonAssert.fail.called);491 });492 it("passes when method doesn't fail", function() {493 sinonStub(this.stub, "calledWithNew").returns(true);494 var stub = this.stub;495 sinonAssert.calledWithNew(stub);496 assert.isFalse(sinonAssert.fail.called);497 });498 it("calls pass callback", function() {499 new this.stub(); // eslint-disable-line no-new, new-cap500 sinonAssert.calledWithNew(this.stub);501 assert(sinonAssert.pass.calledOnce);502 assert(sinonAssert.pass.calledWith("calledWithNew"));503 });504 it("works with spyCall", function() {505 var spy = sinonSpy();506 spy();507 new spy(); // eslint-disable-line no-new, new-cap508 sinonAssert.calledWithNew(spy.lastCall);509 assert(sinonAssert.pass.calledWithNew);510 assert(sinonAssert.pass.calledWith("calledWithNew"));511 });512 it("fails when spyCall failed", function() {513 var spy = sinonSpy();514 spy();515 new spy(); // eslint-disable-line no-new, new-cap516 assert.exception(function() {517 sinonAssert.calledWithNew(spy.firstCall);518 });519 assert(sinonAssert.fail.called);520 });521 });522 describe(".alwaysCalledWithNew", function() {523 // eslint-disable-next-line mocha/no-setup-in-describe524 requiresValidFake("alwaysCalledWithNew");525 it("fails when method does not exist", function() {526 sinonStub(this.stub, "alwaysCalledWithNew");527 assert.exception(function() {528 sinonAssert.alwaysCalledWithNew(null);529 });530 assert.isFalse(this.stub.alwaysCalledWithNew.called);531 assert(sinonAssert.fail.called);532 });533 it("fails when method is not stub", function() {534 sinonStub(this.stub, "alwaysCalledWithNew");535 assert.exception(function() {536 sinonAssert.alwaysCalledWithNew(function() {537 return;538 });539 });540 assert.isFalse(this.stub.alwaysCalledWithNew.called);541 assert(sinonAssert.fail.called);542 });543 it("fails when method fails", function() {544 sinonStub(this.stub, "alwaysCalledWithNew").returns(false);545 var stub = this.stub;546 assert.exception(function() {547 sinonAssert.alwaysCalledWithNew(stub);548 });549 assert(sinonAssert.fail.called);550 });551 it("passes when method doesn't fail", function() {552 sinonStub(this.stub, "alwaysCalledWithNew").returns(true);553 var stub = this.stub;554 sinonAssert.alwaysCalledWithNew(stub);555 assert.isFalse(sinonAssert.fail.called);556 });557 it("calls pass callback", function() {558 new this.stub(); // eslint-disable-line no-new, new-cap559 sinonAssert.alwaysCalledWithNew(this.stub);560 assert(sinonAssert.pass.calledOnce);561 assert(sinonAssert.pass.calledWith("alwaysCalledWithNew"));562 });563 });564 describe(".calledWith", function() {565 it("fails when method fails", function() {566 var object = {};567 sinonStub(this.stub, "calledWith").returns(false);568 var stub = this.stub;569 assert.exception(function() {570 sinonAssert.calledWith(stub, object, 1);571 });572 assert(this.stub.calledWith.calledWith(object, 1));573 assert(sinonAssert.fail.called);574 });575 it("passes when method doesn't fail", function() {576 var object = {};577 sinonStub(this.stub, "calledWith").returns(true);578 var stub = this.stub;579 refute.exception(function() {580 sinonAssert.calledWith(stub, object, 1);581 });582 assert(this.stub.calledWith.calledWith(object, 1));583 assert.isFalse(sinonAssert.fail.called);584 });585 it("calls pass callback", function() {586 this.stub("yeah");587 sinonAssert.calledWith(this.stub, "yeah");588 assert(sinonAssert.pass.calledOnce);589 assert(sinonAssert.pass.calledWith("calledWith"));590 });591 it("works with spyCall", function() {592 var spy = sinonSpy();593 var object = {};594 spy();595 spy(object);596 sinonAssert.calledWith(spy.lastCall, object);597 assert(sinonAssert.pass.calledOnce);598 assert(sinonAssert.pass.calledWith("calledWith"));599 });600 it("fails when spyCall failed", function() {601 var spy = sinonSpy();602 var object = {};603 spy();604 spy(object);605 assert.exception(function() {606 sinonAssert.calledWith(spy.lastCall, 1);607 });608 assert(sinonAssert.fail.called);609 });610 });611 describe(".calledWithExactly", function() {612 it("fails when method fails", function() {613 var object = {};614 sinonStub(this.stub, "calledWithExactly").returns(false);615 var stub = this.stub;616 assert.exception(function() {617 sinonAssert.calledWithExactly(stub, object, 1);618 });619 assert(this.stub.calledWithExactly.calledWithExactly(object, 1));620 assert(sinonAssert.fail.called);621 });622 it("passes when method doesn't fail", function() {623 var object = {};624 sinonStub(this.stub, "calledWithExactly").returns(true);625 var stub = this.stub;626 refute.exception(function() {627 sinonAssert.calledWithExactly(stub, object, 1);628 });629 assert(this.stub.calledWithExactly.calledWithExactly(object, 1));630 assert.isFalse(sinonAssert.fail.called);631 });632 it("calls pass callback", function() {633 this.stub("yeah");634 sinonAssert.calledWithExactly(this.stub, "yeah");635 assert(sinonAssert.pass.calledOnce);636 assert(sinonAssert.pass.calledWith("calledWithExactly"));637 });638 it("works with spyCall", function() {639 var spy = sinonSpy();640 var object = {};641 spy();642 spy(object);643 sinonAssert.calledWithExactly(spy.lastCall, object);644 assert(sinonAssert.pass.calledOnce);645 assert(sinonAssert.pass.calledWith("calledWithExactly"));646 });647 it("fails when spyCall failed", function() {648 var spy = sinonSpy();649 var object = {};650 spy();651 spy(object);652 assert.exception(function() {653 sinonAssert.calledWithExactly(spy.lastCall, 1);654 });655 assert(sinonAssert.fail.called);656 });657 });658 describe(".calledOnceWithExactly", function() {659 // eslint-disable-next-line mocha/no-setup-in-describe660 requiresValidFake("calledOnceWithExactly");661 it("fails when method fails", function() {662 var object = {};663 sinonStub(this.stub, "calledOnceWithExactly").returns(false);664 var stub = this.stub;665 assert.exception(function() {666 sinonAssert.calledOnceWithExactly(stub, object, 1);667 });668 assert(this.stub.calledOnceWithExactly.calledOnceWithExactly(object, 1));669 assert(sinonAssert.fail.called);670 });671 it("passes when method doesn't fail", function() {672 var object = {};673 sinonStub(this.stub, "calledOnceWithExactly").returns(true);674 var stub = this.stub;675 refute.exception(function() {676 sinonAssert.calledOnceWithExactly(stub, object, 1);677 });678 assert(this.stub.calledOnceWithExactly.calledOnceWithExactly(object, 1));679 assert.isFalse(sinonAssert.fail.called);680 });681 it("calls pass callback", function() {682 this.stub("yeah");683 sinonAssert.calledOnceWithExactly(this.stub, "yeah");684 assert(sinonAssert.pass.calledOnce);685 assert(sinonAssert.pass.calledWith("calledOnceWithExactly"));686 });687 it("fails when method does not exist", function() {688 assert.exception(function() {689 sinonAssert.calledOnceWithExactly();690 });691 assert(sinonAssert.fail.called);692 });693 it("fails when method is not stub", function() {694 assert.exception(function() {695 sinonAssert.calledOnceWithExactly(function() {696 return;697 });698 });699 assert(sinonAssert.fail.called);700 });701 it("fails when method was not called", function() {702 var stub = this.stub;703 assert.exception(function() {704 sinonAssert.calledOnceWithExactly(stub);705 });706 assert(sinonAssert.fail.called);707 });708 it("fails when called with more than one argument", function() {709 var stub = this.stub;710 stub();711 assert.exception(function() {712 sinonAssert.calledOnceWithExactly(stub, 1);713 });714 });715 it("passes when method was called", function() {716 var stub = this.stub;717 stub();718 refute.exception(function() {719 sinonAssert.calledOnceWithExactly(stub);720 });721 assert.isFalse(sinonAssert.fail.called);722 });723 it("fails when method was called more than once", function() {724 var stub = this.stub;725 stub();726 stub();727 assert.exception(function() {728 sinonAssert.calledOnceWithExactly(stub);729 });730 assert(sinonAssert.fail.called);731 });732 });733 describe(".calledOnceWithMatch", function() {734 // eslint-disable-next-line mocha/no-setup-in-describe735 requiresValidFake("calledOnceWithMatch");736 it("fails when method fails", function() {737 var object = {};738 sinonStub(this.stub, "calledOnceWithMatch").returns(false);739 var stub = this.stub;740 assert.exception(function() {741 sinonAssert.calledOnceWithMatch(stub, object, 1);742 });743 assert(this.stub.calledOnceWithMatch.calledOnceWithMatch(object, 1));744 assert(sinonAssert.fail.called);745 });746 it("passes when method doesn't fail", function() {747 var object = {};748 sinonStub(this.stub, "calledOnceWithMatch").returns(true);749 var stub = this.stub;750 refute.exception(function() {751 sinonAssert.calledOnceWithMatch(stub, object, 1);752 });753 assert(this.stub.calledOnceWithMatch.calledOnceWithMatch(object, 1));754 assert.isFalse(sinonAssert.fail.called);755 });756 it("calls pass callback", function() {757 this.stub("yeah");758 sinonAssert.calledOnceWithMatch(this.stub, "yeah");759 assert(sinonAssert.pass.calledOnce);760 assert(sinonAssert.pass.calledWith("calledOnceWithMatch"));761 });762 it("fails when method does not exist", function() {763 assert.exception(function() {764 sinonAssert.calledOnceWithMatch();765 });766 assert(sinonAssert.fail.called);767 });768 it("fails when method is not stub", function() {769 assert.exception(function() {770 sinonAssert.calledOnceWithMatch(function() {771 return;772 });773 });774 assert(sinonAssert.fail.called);775 });776 it("fails when method was not called", function() {777 var stub = this.stub;778 assert.exception(function() {779 sinonAssert.calledOnceWithMatch(stub);780 });781 assert(sinonAssert.fail.called);782 });783 it("fails when called with more than one argument", function() {784 var stub = this.stub;785 stub();786 assert.exception(function() {787 sinonAssert.calledOnceWithMatch(stub, 1);788 });789 });790 it("passes when method was called", function() {791 var stub = this.stub;792 stub();793 refute.exception(function() {794 sinonAssert.calledOnceWithMatch(stub);795 });796 assert.isFalse(sinonAssert.fail.called);797 });798 it("fails when method was called more than once", function() {799 var stub = this.stub;800 stub();801 stub();802 assert.exception(function() {803 sinonAssert.calledOnceWithMatch(stub);804 });805 assert(sinonAssert.fail.called);806 });807 });808 describe(".neverCalledWith", function() {809 it("fails when method fails", function() {810 var object = {};811 sinonStub(this.stub, "neverCalledWith").returns(false);812 var stub = this.stub;813 assert.exception(function() {814 sinonAssert.neverCalledWith(stub, object, 1);815 });816 assert(this.stub.neverCalledWith.calledWith(object, 1));817 assert(sinonAssert.fail.called);818 });819 it("passes when method doesn't fail", function() {820 var object = {};821 sinonStub(this.stub, "neverCalledWith").returns(true);822 var stub = this.stub;823 refute.exception(function() {824 sinonAssert.neverCalledWith(stub, object, 1);825 });826 assert(this.stub.neverCalledWith.calledWith(object, 1));827 assert.isFalse(sinonAssert.fail.called);828 });829 it("calls pass callback", function() {830 this.stub("yeah");831 sinonAssert.neverCalledWith(this.stub, "nah!");832 assert(sinonAssert.pass.calledOnce);833 assert(sinonAssert.pass.calledWith("neverCalledWith"));834 });835 });836 describe(".threw", function() {837 it("fails when method fails", function() {838 sinonStub(this.stub, "threw").returns(false);839 var stub = this.stub;840 assert.exception(function() {841 sinonAssert.threw(stub, 1, 2);842 });843 assert(this.stub.threw.calledWithExactly(1, 2));844 assert(sinonAssert.fail.called);845 });846 it("passes when method doesn't fail", function() {847 sinonStub(this.stub, "threw").returns(true);848 var stub = this.stub;849 refute.exception(function() {850 sinonAssert.threw(stub, 1, 2);851 });852 assert(this.stub.threw.calledWithExactly(1, 2));853 assert.isFalse(sinonAssert.fail.called);854 });855 it("calls pass callback", function() {856 sinonStub(this.stub, "threw").returns(true);857 this.stub();858 sinonAssert.threw(this.stub);859 assert(sinonAssert.pass.calledOnce);860 assert(sinonAssert.pass.calledWith("threw"));861 });862 it("works with spyCall", function() {863 var stub = sinonStub().throws("Error");864 assert.exception(function() {865 stub();866 });867 sinonAssert.threw(stub.firstCall, "Error");868 assert(sinonAssert.pass.threw);869 assert(sinonAssert.pass.calledWith("threw"));870 });871 it("fails when spyCall failed", function() {872 var stub = sinonStub().returns("Error");873 stub();874 assert.exception(function() {875 sinonAssert.threw(stub.firstCall, "Error");876 });877 assert(sinonAssert.fail.called);878 });879 });880 describe(".callCount", function() {881 // eslint-disable-next-line mocha/no-setup-in-describe882 requiresValidFake("callCount");883 it("fails when method fails", function() {884 this.stub();885 this.stub();886 var stub = this.stub;887 assert.exception(function() {888 sinonAssert.callCount(stub, 3);889 });890 assert(sinonAssert.fail.called);891 });892 it("passes when method doesn't fail", function() {893 var stub = this.stub;894 this.stub.callCount = 3;895 refute.exception(function() {896 sinonAssert.callCount(stub, 3);...

Full Screen

Full Screen

96assert-test.js

Source:96assert-test.js Github

copy

Full Screen

...6var sinonAssert = require("../lib/sinon/assert");7var sinonMatch = require("../lib/sinon/match");8var assert = referee.assert;9var refute = referee.refute;10function requiresValidFake(method) {11 it("should fail with non-function fake", function() {12 assert.exception(function() {13 sinonAssert[method]({});14 });15 });16}17describe("assert", function() {18 beforeEach(function() {19 this.global = typeof window !== "undefined" ? window : global;20 this.setUpStubs = function() {21 this.stub = sinonStub.create();22 sinonStub(sinonAssert, "fail").throws();23 sinonStub(sinonAssert, "pass");24 };25 this.tearDownStubs = function() {26 sinonAssert.fail.restore();27 sinonAssert.pass.restore();28 };29 });30 it("is object", function() {31 assert.isObject(sinonAssert);32 });33 it("supports proxy property", function() {34 var api = {35 method: function() {36 return;37 }38 };39 api.method.proxy = function() {40 return;41 };42 sinonSpy(api, "method");43 api.method();44 refute.exception(function() {45 sinonAssert.calledOnce(api.method);46 });47 });48 describe(".fail", function() {49 beforeEach(function() {50 this.exceptionName = sinonAssert.failException;51 });52 afterEach(function() {53 sinonAssert.failException = this.exceptionName;54 });55 it("throws exception", function() {56 assert.exception(57 function() {58 sinonAssert.fail("Some message");59 },60 {61 name: "AssertError"62 }63 );64 });65 it("throws configured exception type", function() {66 sinonAssert.failException = "CustomError";67 assert.exception(68 function() {69 sinonAssert.fail("Some message");70 },71 { name: "CustomError" }72 );73 });74 });75 describe("with stubs", function() {76 beforeEach(function() {77 this.setUpStubs();78 });79 afterEach(function() {80 this.tearDownStubs();81 });82 describe(".match", function() {83 it("fails when arguments to not match", function() {84 assert.exception(function() {85 sinonAssert.match("foo", "bar");86 });87 assert(sinonAssert.fail.calledOnce);88 });89 it("passes when argumens match", function() {90 sinonAssert.match("foo", "foo");91 assert(sinonAssert.pass.calledOnce);92 });93 });94 describe(".called", function() {95 requiresValidFake("called");96 it("fails when method does not exist", function() {97 assert.exception(function() {98 sinonAssert.called();99 });100 assert(sinonAssert.fail.called);101 });102 it("fails when method is not stub", function() {103 assert.exception(function() {104 sinonAssert.called(function() {105 return;106 });107 });108 assert(sinonAssert.fail.called);109 });110 it("fails when method was not called", function() {111 var stub = this.stub;112 assert.exception(function() {113 sinonAssert.called(stub);114 });115 assert(sinonAssert.fail.called);116 });117 it("fails when called with more than one argument", function() {118 var stub = this.stub;119 stub();120 assert.exception(function() {121 sinonAssert.called(stub, 1);122 });123 });124 it("does not fail when method was called", function() {125 var stub = this.stub;126 stub();127 refute.exception(function() {128 sinonAssert.called(stub);129 });130 assert.isFalse(sinonAssert.fail.called);131 });132 it("calls pass callback", function() {133 var stub = this.stub;134 stub();135 refute.exception(function() {136 sinonAssert.called(stub);137 });138 assert(sinonAssert.pass.calledOnce);139 assert(sinonAssert.pass.calledWith("called"));140 });141 });142 describe(".notCalled", function() {143 requiresValidFake("notCalled");144 it("fails when method does not exist", function() {145 assert.exception(function() {146 sinonAssert.notCalled();147 });148 assert(sinonAssert.fail.called);149 });150 it("fails when method is not stub", function() {151 assert.exception(function() {152 sinonAssert.notCalled(function() {153 return;154 });155 });156 assert(sinonAssert.fail.called);157 });158 it("fails when method was called", function() {159 var stub = this.stub;160 stub();161 assert.exception(function() {162 sinonAssert.notCalled(stub);163 });164 assert(sinonAssert.fail.called);165 });166 it("fails when called with more than one argument", function() {167 var stub = this.stub;168 assert.exception(function() {169 sinonAssert.notCalled(stub, 1);170 });171 });172 it("passes when method was not called", function() {173 var stub = this.stub;174 refute.exception(function() {175 sinonAssert.notCalled(stub);176 });177 assert.isFalse(sinonAssert.fail.called);178 });179 it("should call pass callback", function() {180 var stub = this.stub;181 sinonAssert.notCalled(stub);182 assert(sinonAssert.pass.calledOnce);183 assert(sinonAssert.pass.calledWith("notCalled"));184 });185 });186 describe(".calledOnce", function() {187 requiresValidFake("calledOnce");188 it("fails when method does not exist", function() {189 assert.exception(function() {190 sinonAssert.calledOnce();191 });192 assert(sinonAssert.fail.called);193 });194 it("fails when method is not stub", function() {195 assert.exception(function() {196 sinonAssert.calledOnce(function() {197 return;198 });199 });200 assert(sinonAssert.fail.called);201 });202 it("fails when method was not called", function() {203 var stub = this.stub;204 assert.exception(function() {205 sinonAssert.calledOnce(stub);206 });207 assert(sinonAssert.fail.called);208 });209 it("fails when called with more than one argument", function() {210 var stub = this.stub;211 stub();212 assert.exception(function() {213 sinonAssert.calledOnce(stub, 1);214 });215 });216 it("passes when method was called", function() {217 var stub = this.stub;218 stub();219 refute.exception(function() {220 sinonAssert.calledOnce(stub);221 });222 assert.isFalse(sinonAssert.fail.called);223 });224 it("fails when method was called more than once", function() {225 var stub = this.stub;226 stub();227 stub();228 assert.exception(function() {229 sinonAssert.calledOnce(stub);230 });231 assert(sinonAssert.fail.called);232 });233 it("calls pass callback", function() {234 var stub = this.stub;235 stub();236 sinonAssert.calledOnce(stub);237 assert(sinonAssert.pass.calledOnce);238 assert(sinonAssert.pass.calledWith("calledOnce"));239 });240 });241 describe(".calledTwice", function() {242 requiresValidFake("calledTwice");243 it("fails if called once", function() {244 var stub = this.stub;245 this.stub();246 assert.exception(function() {247 sinonAssert.calledTwice(stub);248 });249 });250 it("fails when called with more than one argument", function() {251 var stub = this.stub;252 this.stub();253 this.stub();254 assert.exception(function() {255 sinonAssert.calledTwice(stub, 1);256 });257 });258 it("passes if called twice", function() {259 var stub = this.stub;260 this.stub();261 this.stub();262 refute.exception(function() {263 sinonAssert.calledTwice(stub);264 });265 });266 it("calls pass callback", function() {267 var stub = this.stub;268 stub();269 stub();270 sinonAssert.calledTwice(stub);271 assert(sinonAssert.pass.calledOnce);272 assert(sinonAssert.pass.calledWith("calledTwice"));273 });274 });275 describe(".calledThrice", function() {276 requiresValidFake("calledThrice");277 it("fails if called once", function() {278 var stub = this.stub;279 this.stub();280 assert.exception(function() {281 sinonAssert.calledThrice(stub);282 });283 });284 it("fails when called with more than one argument", function() {285 var stub = this.stub;286 this.stub();287 this.stub();288 this.stub();289 assert.exception(function() {290 sinonAssert.calledThrice(stub, 1);291 });292 });293 it("passes if called thrice", function() {294 var stub = this.stub;295 this.stub();296 this.stub();297 this.stub();298 refute.exception(function() {299 sinonAssert.calledThrice(stub);300 });301 });302 it("calls pass callback", function() {303 var stub = this.stub;304 stub();305 stub();306 stub();307 sinonAssert.calledThrice(stub);308 assert(sinonAssert.pass.calledOnce);309 assert(sinonAssert.pass.calledWith("calledThrice"));310 });311 });312 describe(".callOrder", function() {313 it("passes when calls were done in right order", function() {314 var spy1 = sinonSpy();315 var spy2 = sinonSpy();316 spy1();317 spy2();318 refute.exception(function() {319 sinonAssert.callOrder(spy1, spy2);320 });321 });322 it("fails when calls were done in wrong order", function() {323 var spy1 = sinonSpy();324 var spy2 = sinonSpy();325 spy2();326 spy1();327 assert.exception(function() {328 sinonAssert.callOrder(spy1, spy2);329 });330 assert(sinonAssert.fail.called);331 });332 it("passes when many calls were done in right order", function() {333 var spy1 = sinonSpy();334 var spy2 = sinonSpy();335 var spy3 = sinonSpy();336 var spy4 = sinonSpy();337 spy1();338 spy2();339 spy3();340 spy4();341 refute.exception(function() {342 sinonAssert.callOrder(spy1, spy2, spy3, spy4);343 });344 });345 it("fails when one of many calls were done in wrong order", function() {346 var spy1 = sinonSpy();347 var spy2 = sinonSpy();348 var spy3 = sinonSpy();349 var spy4 = sinonSpy();350 spy1();351 spy2();352 spy4();353 spy3();354 assert.exception(function() {355 sinonAssert.callOrder(spy1, spy2, spy3, spy4);356 });357 assert(sinonAssert.fail.called);358 });359 it("calls pass callback", function() {360 var stubs = [sinonSpy(), sinonSpy()];361 stubs[0]();362 stubs[1]();363 sinonAssert.callOrder(stubs[0], stubs[1]);364 assert(sinonAssert.pass.calledOnce);365 assert(sinonAssert.pass.calledWith("callOrder"));366 });367 it("passes for multiple calls to same spy", function() {368 var first = sinonSpy();369 var second = sinonSpy();370 first();371 second();372 first();373 refute.exception(function() {374 sinonAssert.callOrder(first, second, first);375 });376 });377 it("fails if first spy was not called", function() {378 var first = sinonSpy();379 var second = sinonSpy();380 second();381 assert.exception(function() {382 sinonAssert.callOrder(first, second);383 });384 });385 it("fails if second spy was not called", function() {386 var first = sinonSpy();387 var second = sinonSpy();388 first();389 assert.exception(function() {390 sinonAssert.callOrder(first, second);391 });392 });393 });394 describe(".calledOn", function() {395 it("fails when method does not exist", function() {396 var object = {};397 sinonStub(this.stub, "calledOn");398 assert.exception(function() {399 sinonAssert.calledOn(null, object);400 });401 assert.isFalse(this.stub.calledOn.calledWith(object));402 assert(sinonAssert.fail.called);403 });404 it("fails when method is not stub", function() {405 var object = {};406 sinonStub(this.stub, "calledOn");407 assert.exception(function() {408 sinonAssert.calledOn(function() {409 return;410 }, object);411 });412 assert.isFalse(this.stub.calledOn.calledWith(object));413 assert(sinonAssert.fail.called);414 });415 it("fails when method fails", function() {416 var object = {};417 sinonStub(this.stub, "calledOn").returns(false);418 var stub = this.stub;419 assert.exception(function() {420 sinonAssert.calledOn(stub, object);421 });422 assert(sinonAssert.fail.called);423 });424 it("passes when method doesn't fail", function() {425 var object = {};426 sinonStub(this.stub, "calledOn").returns(true);427 var stub = this.stub;428 sinonAssert.calledOn(stub, object);429 assert.isFalse(sinonAssert.fail.called);430 });431 it("calls pass callback", function() {432 var obj = {};433 this.stub.call(obj);434 sinonAssert.calledOn(this.stub, obj);435 assert(sinonAssert.pass.calledOnce);436 assert(sinonAssert.pass.calledWith("calledOn"));437 });438 it("works with spyCall", function() {439 var spy = sinonSpy();440 var target = {};441 spy();442 spy.call(target);443 sinonAssert.calledOn(spy.lastCall, target);444 assert(sinonAssert.pass.calledOn);445 assert(sinonAssert.pass.calledWith("calledOn"));446 });447 it("fails when spyCall failed", function() {448 var spy = sinonSpy();449 var target = {};450 spy();451 spy.call(target);452 assert.exception(function() {453 sinonAssert.calledOn(spy.lastCall, 1);454 });455 assert(sinonAssert.fail.called);456 });457 });458 describe(".calledWithNew", function() {459 requiresValidFake("calledWithNew");460 it("fails when method does not exist", function() {461 sinonStub(this.stub, "calledWithNew");462 assert.exception(function() {463 sinonAssert.calledWithNew(null);464 });465 assert.isFalse(this.stub.calledWithNew.called);466 assert(sinonAssert.fail.called);467 });468 it("fails when method is not stub", function() {469 sinonStub(this.stub, "calledWithNew");470 assert.exception(function() {471 sinonAssert.calledWithNew(function() {472 return;473 });474 });475 assert.isFalse(this.stub.calledWithNew.called);476 assert(sinonAssert.fail.called);477 });478 it("fails when method fails", function() {479 sinonStub(this.stub, "calledWithNew").returns(false);480 var stub = this.stub;481 assert.exception(function() {482 sinonAssert.calledWithNew(stub);483 });484 assert(sinonAssert.fail.called);485 });486 it("passes when method doesn't fail", function() {487 sinonStub(this.stub, "calledWithNew").returns(true);488 var stub = this.stub;489 sinonAssert.calledWithNew(stub);490 assert.isFalse(sinonAssert.fail.called);491 });492 it("calls pass callback", function() {493 new this.stub(); // eslint-disable-line no-new, new-cap494 sinonAssert.calledWithNew(this.stub);495 assert(sinonAssert.pass.calledOnce);496 assert(sinonAssert.pass.calledWith("calledWithNew"));497 });498 it("works with spyCall", function() {499 var spy = sinonSpy();500 spy();501 new spy(); // eslint-disable-line no-new, new-cap502 sinonAssert.calledWithNew(spy.lastCall);503 assert(sinonAssert.pass.calledWithNew);504 assert(sinonAssert.pass.calledWith("calledWithNew"));505 });506 it("fails when spyCall failed", function() {507 var spy = sinonSpy();508 spy();509 new spy(); // eslint-disable-line no-new, new-cap510 assert.exception(function() {511 sinonAssert.calledWithNew(spy.firstCall);512 });513 assert(sinonAssert.fail.called);514 });515 });516 describe(".alwaysCalledWithNew", function() {517 requiresValidFake("alwaysCalledWithNew");518 it("fails when method does not exist", function() {519 sinonStub(this.stub, "alwaysCalledWithNew");520 assert.exception(function() {521 sinonAssert.alwaysCalledWithNew(null);522 });523 assert.isFalse(this.stub.alwaysCalledWithNew.called);524 assert(sinonAssert.fail.called);525 });526 it("fails when method is not stub", function() {527 sinonStub(this.stub, "alwaysCalledWithNew");528 assert.exception(function() {529 sinonAssert.alwaysCalledWithNew(function() {530 return;531 });532 });533 assert.isFalse(this.stub.alwaysCalledWithNew.called);534 assert(sinonAssert.fail.called);535 });536 it("fails when method fails", function() {537 sinonStub(this.stub, "alwaysCalledWithNew").returns(false);538 var stub = this.stub;539 assert.exception(function() {540 sinonAssert.alwaysCalledWithNew(stub);541 });542 assert(sinonAssert.fail.called);543 });544 it("passes when method doesn't fail", function() {545 sinonStub(this.stub, "alwaysCalledWithNew").returns(true);546 var stub = this.stub;547 sinonAssert.alwaysCalledWithNew(stub);548 assert.isFalse(sinonAssert.fail.called);549 });550 it("calls pass callback", function() {551 new this.stub(); // eslint-disable-line no-new, new-cap552 sinonAssert.alwaysCalledWithNew(this.stub);553 assert(sinonAssert.pass.calledOnce);554 assert(sinonAssert.pass.calledWith("alwaysCalledWithNew"));555 });556 });557 describe(".calledWith", function() {558 it("fails when method fails", function() {559 var object = {};560 sinonStub(this.stub, "calledWith").returns(false);561 var stub = this.stub;562 assert.exception(function() {563 sinonAssert.calledWith(stub, object, 1);564 });565 assert(this.stub.calledWith.calledWith(object, 1));566 assert(sinonAssert.fail.called);567 });568 it("passes when method doesn't fail", function() {569 var object = {};570 sinonStub(this.stub, "calledWith").returns(true);571 var stub = this.stub;572 refute.exception(function() {573 sinonAssert.calledWith(stub, object, 1);574 });575 assert(this.stub.calledWith.calledWith(object, 1));576 assert.isFalse(sinonAssert.fail.called);577 });578 it("calls pass callback", function() {579 this.stub("yeah");580 sinonAssert.calledWith(this.stub, "yeah");581 assert(sinonAssert.pass.calledOnce);582 assert(sinonAssert.pass.calledWith("calledWith"));583 });584 it("works with spyCall", function() {585 var spy = sinonSpy();586 var object = {};587 spy();588 spy(object);589 sinonAssert.calledWith(spy.lastCall, object);590 assert(sinonAssert.pass.calledOnce);591 assert(sinonAssert.pass.calledWith("calledWith"));592 });593 it("fails when spyCall failed", function() {594 var spy = sinonSpy();595 var object = {};596 spy();597 spy(object);598 assert.exception(function() {599 sinonAssert.calledWith(spy.lastCall, 1);600 });601 assert(sinonAssert.fail.called);602 });603 });604 describe(".calledWithExactly", function() {605 it("fails when method fails", function() {606 var object = {};607 sinonStub(this.stub, "calledWithExactly").returns(false);608 var stub = this.stub;609 assert.exception(function() {610 sinonAssert.calledWithExactly(stub, object, 1);611 });612 assert(this.stub.calledWithExactly.calledWithExactly(object, 1));613 assert(sinonAssert.fail.called);614 });615 it("passes when method doesn't fail", function() {616 var object = {};617 sinonStub(this.stub, "calledWithExactly").returns(true);618 var stub = this.stub;619 refute.exception(function() {620 sinonAssert.calledWithExactly(stub, object, 1);621 });622 assert(this.stub.calledWithExactly.calledWithExactly(object, 1));623 assert.isFalse(sinonAssert.fail.called);624 });625 it("calls pass callback", function() {626 this.stub("yeah");627 sinonAssert.calledWithExactly(this.stub, "yeah");628 assert(sinonAssert.pass.calledOnce);629 assert(sinonAssert.pass.calledWith("calledWithExactly"));630 });631 it("works with spyCall", function() {632 var spy = sinonSpy();633 var object = {};634 spy();635 spy(object);636 sinonAssert.calledWithExactly(spy.lastCall, object);637 assert(sinonAssert.pass.calledOnce);638 assert(sinonAssert.pass.calledWith("calledWithExactly"));639 });640 it("fails when spyCall failed", function() {641 var spy = sinonSpy();642 var object = {};643 spy();644 spy(object);645 assert.exception(function() {646 sinonAssert.calledWithExactly(spy.lastCall, 1);647 });648 assert(sinonAssert.fail.called);649 });650 });651 describe(".neverCalledWith", function() {652 it("fails when method fails", function() {653 var object = {};654 sinonStub(this.stub, "neverCalledWith").returns(false);655 var stub = this.stub;656 assert.exception(function() {657 sinonAssert.neverCalledWith(stub, object, 1);658 });659 assert(this.stub.neverCalledWith.calledWith(object, 1));660 assert(sinonAssert.fail.called);661 });662 it("passes when method doesn't fail", function() {663 var object = {};664 sinonStub(this.stub, "neverCalledWith").returns(true);665 var stub = this.stub;666 refute.exception(function() {667 sinonAssert.neverCalledWith(stub, object, 1);668 });669 assert(this.stub.neverCalledWith.calledWith(object, 1));670 assert.isFalse(sinonAssert.fail.called);671 });672 it("calls pass callback", function() {673 this.stub("yeah");674 sinonAssert.neverCalledWith(this.stub, "nah!");675 assert(sinonAssert.pass.calledOnce);676 assert(sinonAssert.pass.calledWith("neverCalledWith"));677 });678 });679 describe(".threwTest", function() {680 it("fails when method fails", function() {681 sinonStub(this.stub, "threw").returns(false);682 var stub = this.stub;683 assert.exception(function() {684 sinonAssert.threw(stub, 1, 2);685 });686 assert(this.stub.threw.calledWithExactly(1, 2));687 assert(sinonAssert.fail.called);688 });689 it("passes when method doesn't fail", function() {690 sinonStub(this.stub, "threw").returns(true);691 var stub = this.stub;692 refute.exception(function() {693 sinonAssert.threw(stub, 1, 2);694 });695 assert(this.stub.threw.calledWithExactly(1, 2));696 assert.isFalse(sinonAssert.fail.called);697 });698 it("calls pass callback", function() {699 sinonStub(this.stub, "threw").returns(true);700 this.stub();701 sinonAssert.threw(this.stub);702 assert(sinonAssert.pass.calledOnce);703 assert(sinonAssert.pass.calledWith("threw"));704 });705 it("works with spyCall", function() {706 var stub = sinonStub().throws("Error");707 assert.exception(function() {708 stub();709 });710 sinonAssert.threw(stub.firstCall, "Error");711 assert(sinonAssert.pass.threw);712 assert(sinonAssert.pass.calledWith("threw"));713 });714 it("fails when spyCall failed", function() {715 var stub = sinonStub().returns("Error");716 stub();717 assert.exception(function() {718 sinonAssert.threw(stub.firstCall, "Error");719 });720 assert(sinonAssert.fail.called);721 });722 });723 describe(".callCount", function() {724 requiresValidFake("callCount");725 it("fails when method fails", function() {726 this.stub();727 this.stub();728 var stub = this.stub;729 assert.exception(function() {730 sinonAssert.callCount(stub, 3);731 });732 assert(sinonAssert.fail.called);733 });734 it("passes when method doesn't fail", function() {735 var stub = this.stub;736 this.stub.callCount = 3;737 refute.exception(function() {738 sinonAssert.callCount(stub, 3);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var assert = require('assert');3var fake = sinon.fake();4fake(1, 2, 3);5sinon.assert.calledWith(fake, 1, 2, 3);6sinon.assert.calledWith(fake, 1, 2);7sinon.assert.calledWith(fake, 1);8sinon.assert.calledWith(fake, 1, 2, 3, 4);9sinon.assert.calledWith(fake, 1, 2, 3, 4, 5);10var sinon = require('sinon');11var assert = require('assert');12var fake = sinon.fake();13fake(1, 2, 3);14assert.calledWith(fake, 1, 2, 3);15assert.calledWith(fake, 1, 2);16assert.calledWith(fake, 1);17assert.calledWith(fake, 1, 2, 3, 4);18assert.calledWith(fake, 1, 2, 3, 4, 5);

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var fake = sinon.fake();3sinon.assert.expose(assert, {prefix: ''});4assert.requiresValidFake(fake);5assert.requiresValidFake(function() {});6assert.requiresValidFake(sinon.fake());7assert.requiresValidFake(sinon.fake.returns(42));8assert.requiresValidFake(sinon.fake.returnsArg(0));9assert.requiresValidFake(sinon.fake.throws('TypeError'));10assert.requiresValidFake(sinon.fake.yields('TypeError'));11assert.requiresValidFake(sinon.fake.yieldsTo('onSuccess', 'TypeError'));12assert.requiresValidFake(sinon.fake.resolves('TypeError'));13assert.requiresValidFake(sinon.fake.rejects('TypeError'));14var sinon = require('sinon');15var fake = sinon.fake();16sinon.assert.expose(assert, {prefix: ''});17assert.requiresFake(fake);18assert.requiresFake(function() {});19assert.requiresFake(sinon.fake());20assert.requiresFake(sinon.fake.returns(42));21assert.requiresFake(sinon.fake.returnsArg(0));22assert.requiresFake(sinon.fake.throws('TypeError'));23assert.requiresFake(sinon.fake.yields('TypeError'));24assert.requiresFake(sinon.fake.yieldsTo('onSuccess', 'TypeError'));25assert.requiresFake(sinon.fake.resolves('TypeError'));26assert.requiresFake(sinon.fake.rejects('TypeError'));27var sinon = require('sinon');28var fake = sinon.fake();29sinon.assert.expose(assert, {prefix: ''});30assert.requiresFakes(fake);31assert.requiresFakes(function() {});32assert.requiresFakes(sinon.fake());33assert.requiresFakes(sinon.fake.returns(42));34assert.requiresFakes(sinon.fake.returnsArg(0));35assert.requiresFakes(sinon.fake.throws('TypeError'));36assert.requiresFakes(sinon.fake.yields('TypeError'));37assert.requiresFakes(sinon.fake.yieldsTo('onSuccess', 'TypeError'));38assert.requiresFakes(sinon.fake.resolves('TypeError'));39assert.requiresFakes(sinon.fake.rejects('TypeError'));40var sinon = require('sinon');41var fake = sinon.fake();42sinon.assert.expose(assert, {prefix: ''});43assert.isFake(fake);44assert.isFake(function() {});45assert.isFake(sinon.fake());46assert.isFake(sinon.fake.returns(42));47assert.isFake(sinon.fake.returnsArg(0));

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var assert = require('assert');3var fake = sinon.fake();4sinon.replace(console, 'log', fake);5console.log('Hello World!');6assert(fake.calledOnce);7sinon.restore();8console.log('Hello World!');9assert(fake.calledOnce);10var sinon = require('sinon');11var assert = require('assert');12var fake = sinon.fake();13sinon.replace(console, 'log', fake);14console.log('Hello World!');15assert(fake.calledOnce);16sinon.restore();17console.log('Hello World!');18assert(fake.calledOnce);

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var assert = require('assert');3function testFunction(callback) {4 callback();5}6describe('testFunction', function () {7 it('should call callback', function () {8 var fake = sinon.fake();9 testFunction(fake);10 sinon.assert.calledOnce(fake);11 });12});13var sinon = require('sinon');14var assert = require('assert');15function testFunction(callback) {16 callback();17}18describe('testFunction', function () {19 it('should call callback', function () {20 var fake = sinon.fake();21 testFunction(fake);22 sinon.assert.calledOnce(fake);23 });24});25var sinon = require('sinon');26var assert = require('assert');27function testFunction(callback) {28 callback();29}30describe('testFunction', function () {31 it('should call callback', function () {32 var fake = sinon.fake();33 testFunction(fake);34 sinon.assert.calledOnce(fake);35 });36});37var sinon = require('sinon');38var assert = require('assert');39function testFunction(callback) {40 callback();41}42describe('testFunction', function () {43 it('should call callback', function () {44 var fake = sinon.fake();45 testFunction(fake);46 sinon.assert.calledOnce(fake);47 });48});49var sinon = require('sinon');50var assert = require('assert');51function testFunction(callback) {52 callback();53}54describe('testFunction', function () {55 it('should call callback', function () {56 var fake = sinon.fake();57 testFunction(fake);58 sinon.assert.calledOnce(fake);59 });60});61var sinon = require('sinon');62var assert = require('assert');63function testFunction(callback) {64 callback();65}66describe('testFunction', function () {67 it('should call callback', function () {68 var fake = sinon.fake();69 testFunction(fake);70 sinon.assert.calledOnce(fake);71 });72});73var sinon = require('sinon

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var fake = sinon.fake();3sinon.fake.returns(42);4sinon.fake.throws(new TypeError('invalid'));5sinon.fake.returnsArg(2);6sinon.fake.returnsThis();7sinon.fake.yieldsTo('success');8sinon.fake.yieldsToOn('success', thisArg, 1, 2);9sinon.fake.yieldsAsync(1, 2);10sinon.fake.yieldsAsyncOn(thisArg, 1, 2);11sinon.fake.yieldsToAsync('success');12sinon.fake.yieldsToAsyncOn('success', thisArg, 1, 2);13sinon.fake.resolves(42);14sinon.fake.resolvesArg(2);15sinon.fake.resolvesThis();16sinon.fake.rejects(new TypeError('invalid'));17sinon.fake.rejectsArg(2);18sinon.fake.rejectsThis();19var sinon = require('sinon');20var fake = sinon.fake();21sinon.fake.restore();22sinon.fake.reset();23sinon.fake.resetBehavior();24sinon.fake.resetHistory();25var sinon = require('sinon');26var stub = sinon.stub();27var stub = sinon.stub(object, 'method');28var stub = sinon.stub(ob

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var fake = sinon.fake();3sinon.fake.throws(new Error('invalid fake'));4sinon.fake.returns('fake');5sinon.fake.returnsArg(0);6sinon.fake.returnsThis();7sinon.fake.yields();8sinon.fake.yields(1);9sinon.fake.yields(1, 2);10sinon.fake.yieldsTo('callback');11sinon.fake.yieldsTo('callback', 1);12sinon.fake.yieldsTo('callback', 1, 2);13sinon.fake.yieldsAsync();14sinon.fake.yieldsAsync(1);15sinon.fake.yieldsAsync(1, 2);16sinon.fake.yieldsToAsync('callback');17sinon.fake.yieldsToAsync('callback', 1);18sinon.fake.yieldsToAsync('callback', 1, 2);19sinon.fake.resolves();20sinon.fake.resolves(1);21sinon.fake.resolves(1, 2);22sinon.fake.rejects();23sinon.fake.rejects(1);24sinon.fake.rejects(1, 2);25sinon.fake.withArgs(1, 2);26sinon.fake.withArgs(1, 2).returns('fake');27sinon.fake.withArgs(1, 2).throws(new Error('invalid fake'));28sinon.fake.withArgs(1, 2).resolves();29sinon.fake.withArgs(1, 2).rejects();30sinon.fake.withArgs(1, 2).yields();31sinon.fake.withArgs(1, 2).yields(1);32sinon.fake.withArgs(1, 2).yields(1, 2);33sinon.fake.withArgs(1, 2).yieldsTo('callback');34sinon.fake.withArgs(1, 2).yieldsTo('callback', 1);35sinon.fake.withArgs(1, 2).yieldsTo('callback', 1, 2);36sinon.fake.withArgs(1, 2).yieldsAsync();37sinon.fake.withArgs(1, 2).yieldsAsync(1);38sinon.fake.withArgs(1, 2).yieldsAsync(1, 2);39sinon.fake.withArgs(1, 2).yieldsToAsync('callback');40sinon.fake.withArgs(1, 2).yieldsToAsync('callback', 1

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var fake = {3 method: function () {4 return 'fake';5 }6};7var stub = sinon.stub(fake, 'method');8console.log(fake.method());9stub.requiresValidFake();10console.log(fake.method());11stub.restore();12console.log(fake.method());13Example 2: Using sinon.stub().requiresValidFake() with fake14var sinon = require('sinon');15var fake = {16 method: function () {17 return 'fake';18 }19};20var stub = sinon.stub(fake, 'method');21console.log(fake.method());22stub.requiresValidFake();23console.log(fake.method());24stub.restore();25console.log(fake.method());26var fake = sinon.fake();27console.log(fake.method());28stub.requiresValidFake();29console.log(fake.method());30 at Object.<anonymous> (/home/sunil/sinon/test.js:28:24)31 at Module._compile (module.js:652:30)32 at Object.Module._extensions..js (module.js:663:10)33 at Module.load (module.js:565:32)34 at tryModuleLoad (module.js:505:12)35 at Function.Module._load (module.js:497:3)36 at Function.Module.runMain (module.js:693:10)37 at startup (bootstrap_node.js:191:16)38Recommended Posts: Sinon.JS - sinon.stub().throws()

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var myFake = sinon.fake();3sinon.replace(myFake, 'myFake', sinon.fake.returns('fake'));4myFake();5sinon.assert.calledOnce(myFake);6sinon.assert.calledOnce(myFake.myFake);7sinon.assert.calledOnce(myFake.myFake.myFake);8sinon.assert.calledOnce(myFake.myFake.myFake.myFake);9sinon.assert.calledOnce(myFake.myFake.myFake.myFake.myFake);10sinon.assert.calledOnce(myFake.myFake.myFake.myFake.myFake.myFake);11sinon.assert.calledOnce(myFake.myFake.myFake.myFake.myFake.myFake.myFake);12sinon.assert.calledOnce(myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake);13sinon.assert.calledOnce(myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake);14sinon.assert.calledOnce(myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake);15sinon.assert.calledOnce(myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake);16sinon.assert.calledOnce(myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake);17sinon.assert.calledOnce(myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake);18sinon.assert.calledOnce(myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake);19sinon.assert.calledOnce(myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake.myFake);

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run sinon automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful