How to use createStub method in sinon

Best JavaScript code snippet using sinon

stub-test.js

Source:stub-test.js Github

copy

Full Screen

...11var fail = referee.fail;12var Promise = require("native-promise-only"); // eslint-disable-line no-unused-vars13describe("stub", function () {14 beforeEach(function () {15 createStub(deprecated, "printWarning");16 });17 afterEach(function () {18 if (deprecated.printWarning.restore) {19 deprecated.printWarning.restore();20 }21 });22 it("is spy", function () {23 var stub = createStub.create();24 assert.isFalse(stub.called);25 assert.isFunction(stub.calledWith);26 assert.isFunction(stub.calledOn);27 });28 it("fails if stubbing property on null", function () {29 assert.exception(30 function () {31 createStub(null, "prop");32 },33 {34 message: "Trying to stub property 'prop' of null"35 }36 );37 });38 it("fails if called with an empty property descriptor", function () {39 var propertyKey = "ea762c6d-16ab-4ded-8bc2-3bc6f2de2925";40 var object = {};41 object[propertyKey] = "257b38d8-3c02-4353-82ab-b1b588be6990";42 assert.exception(43 function () {44 createStub(object, propertyKey, {});45 },46 {47 message: "Expected property descriptor to have at least one key"48 }49 );50 });51 it("throws a readable error if stubbing Symbol on null", function () {52 if (typeof Symbol === "function") {53 assert.exception(54 function () {55 createStub(null, Symbol());56 },57 {58 message: "Trying to stub property 'Symbol()' of null"59 }60 );61 }62 });63 it("should contain asynchronous versions of callsArg*, and yields* methods", function () {64 var stub = createStub.create();65 var syncVersions = 0;66 var asyncVersions = 0;67 for (var method in stub) {68 if (stub.hasOwnProperty(method) && method.match(/^(callsArg|yields)/)) {69 if (!method.match(/Async/)) {70 syncVersions++;71 } else if (method.match(/Async/)) {72 asyncVersions++;73 }74 }75 }76 assert.same(syncVersions, asyncVersions,77 "Stub prototype should contain same amount of synchronous and asynchronous methods");78 });79 it("should allow overriding async behavior with sync behavior", function () {80 var stub = createStub();81 var callback = createSpy();82 stub.callsArgAsync(1);83 stub.callsArg(1);84 stub(1, callback);85 assert(callback.called);86 });87 it("should works with combination of withArgs arguments", function () {88 var stub = createStub();89 stub.returns(0);90 stub.withArgs(1, 1).returns(2);91 stub.withArgs(1).returns(1);92 assert.equals(stub(), 0);93 assert.equals(stub(1), 1);94 assert.equals(stub(1, 1), 2);95 assert.equals(stub(1, 1, 1), 2);96 assert.equals(stub(2), 0);97 });98 it("should work with combination of withArgs arguments", function () {99 var stub = createStub();100 stub.withArgs(1).returns(42);101 stub(1);102 refute.isNull(stub.withArgs(1).firstCall);103 });104 describe(".returns", function () {105 it("returns specified value", function () {106 var stub = createStub.create();107 var object = {};108 stub.returns(object);109 assert.same(stub(), object);110 });111 it("returns should return stub", function () {112 var stub = createStub.create();113 assert.same(stub.returns(""), stub);114 });115 it("returns undefined", function () {116 var stub = createStub.create();117 refute.defined(stub());118 });119 it("supersedes previous throws", function () {120 var stub = createStub.create();121 stub.throws().returns(1);122 refute.exception(function () {123 stub();124 });125 });126 });127 describe(".resolves", function () {128 afterEach(function () {129 if (Promise.resolve.restore) {130 Promise.resolve.restore();131 }132 });133 it("returns a promise to the specified value", function () {134 var stub = createStub.create();135 var object = {};136 stub.resolves(object);137 return stub().then(function (actual) {138 assert.same(actual, object);139 });140 });141 it("should return the same stub", function () {142 var stub = createStub.create();143 assert.same(stub.resolves(""), stub);144 });145 it("supersedes previous throws", function () {146 var stub = createStub.create();147 stub.throws().resolves(1);148 refute.exception(function () {149 stub();150 });151 });152 it("supersedes previous rejects", function () {153 var stub = createStub.create();154 stub.rejects(Error("should be superseeded")).resolves(1);155 return stub().then();156 });157 it("can be superseded by returns", function () {158 var stub = createStub.create();159 stub.resolves(2).returns(1);160 assert.equals(stub(), 1);161 });162 it("does not invoke Promise.resolve when the behavior is added to the stub", function () {163 var resolveSpy = createSpy(Promise, "resolve");164 var stub = createStub.create();165 stub.resolves(2);166 assert.equals(resolveSpy.callCount, 0);167 });168 });169 describe(".rejects", function () {170 afterEach(function () {171 if (Promise.reject.restore) {172 Promise.reject.restore();173 }174 });175 it("returns a promise which rejects for the specified reason", function () {176 var stub = createStub.create();177 var reason = new Error();178 stub.rejects(reason);179 return stub().then(function () {180 referee.fail("this should not resolve");181 }).catch(function (actual) {182 assert.same(actual, reason);183 });184 });185 it("should return the same stub", function () {186 var stub = createStub.create();187 assert.same(stub.rejects({}), stub);188 });189 it("specifies exception message", function () {190 var stub = createStub.create();191 var message = "Oh no!";192 stub.rejects("Error", message);193 return stub().then(function () {194 referee.fail("Expected stub to reject");195 }).catch(function (reason) {196 assert.equals(reason.message, message);197 });198 });199 it("does not specify exception message if not provided", function () {200 var stub = createStub.create();201 stub.rejects("Error");202 return stub().then(function () {203 referee.fail("Expected stub to reject");204 }).catch(function (reason) {205 assert.equals(reason.message, "");206 });207 });208 it("rejects for a generic reason", function () {209 var stub = createStub.create();210 stub.rejects();211 return stub().then(function () {212 referee.fail("Expected stub to reject");213 }).catch(function (reason) {214 assert.equals(reason.name, "Error");215 });216 });217 it("can be superseded by returns", function () {218 var stub = createStub.create();219 stub.rejects(2).returns(1);220 assert.equals(stub(), 1);221 });222 it("does not invoke Promise.reject when the behavior is added to the stub", function () {223 var rejectSpy = createSpy(Promise, "reject");224 var stub = createStub.create();225 stub.rejects(2);226 assert.equals(rejectSpy.callCount, 0);227 });228 });229 describe(".returnsArg", function () {230 it("returns argument at specified index", function () {231 var stub = createStub.create();232 stub.returnsArg(0);233 var object = {};234 assert.same(stub(object), object);235 });236 it("returns stub", function () {237 var stub = createStub.create();238 assert.same(stub.returnsArg(0), stub);239 });240 it("throws if no index is specified", function () {241 var stub = createStub.create();242 assert.exception(function () {243 stub.returnsArg();244 }, "TypeError");245 });246 });247 describe(".throwsArg", function () {248 it("throws argument at specified index", function () {249 var stub = createStub.create();250 stub.throwsArg(0);251 var expectedError = new Error("The expected error message");252 assert.exception(function () {253 stub(expectedError);254 }, function (err) {255 return err.message === expectedError.message;256 });257 });258 it("returns stub", function () {259 var stub = createStub.create();260 assert.same(stub.throwsArg(0), stub);261 });262 it("throws TypeError if no index is specified", function () {263 var stub = createStub.create();264 assert.exception(function () {265 stub.throwsArg();266 }, "TypeError");267 });268 it("should throw without enough arguments", function () {269 var stub = createStub.create();270 stub.throwsArg(3);271 assert.exception(272 function () {273 stub("only", "two arguments");274 },275 function (error) {276 return error instanceof TypeError277 && error.message ===278 "throwArgs failed: 3 arguments required but only 2 present"279 ;280 }281 );282 });283 it("should work with call-based behavior", function () {284 var stub = createStub.create();285 var expectedError = new Error("catpants");286 stub.returns(1);287 stub.onSecondCall().throwsArg(1);288 refute.exception(function () {289 assert.equals(1, stub(null, expectedError));290 });291 assert.exception(292 function () {293 stub(null, expectedError);294 },295 function (error) {296 return error.message === expectedError.message;297 }298 );299 });300 it("should be reset by .resetBeahvior", function () {301 var stub = createStub.create();302 stub.throwsArg(0);303 stub.resetBehavior();304 refute.exception(function () {305 stub(new Error("catpants"));306 });307 });308 });309 describe(".returnsThis", function () {310 it("stub returns this", function () {311 var instance = {};312 instance.stub = createStub.create();313 instance.stub.returnsThis();314 assert.same(instance.stub(), instance);315 });316 var strictMode = (function () {317 return this;318 }()) === undefined;319 if (strictMode) {320 it("stub returns undefined when detached", function () {321 var stub = createStub.create();322 stub.returnsThis();323 // Due to strict mode, would be `global` otherwise324 assert.same(stub(), undefined);325 });326 }327 it("stub respects call/apply", function () {328 var stub = createStub.create();329 stub.returnsThis();330 var object = {};331 assert.same(stub.call(object), object);332 assert.same(stub.apply(object), object);333 });334 it("returns stub", function () {335 var stub = createStub.create();336 assert.same(stub.returnsThis(), stub);337 });338 });339 describe(".usingPromise", function () {340 it("should exist and be a function", function () {341 var stub = createStub.create();342 assert(stub.usingPromise);343 assert.isFunction(stub.usingPromise);344 });345 it("should return the current stub", function () {346 var stub = createStub.create();347 assert.same(stub.usingPromise(Promise), stub);348 });349 it("should set the promise used by resolve", function () {350 var stub = createStub.create();351 var promise = {352 resolve: createStub.create().callsFake(function (value) {353 return Promise.resolve(value);354 })355 };356 var object = {};357 stub.usingPromise(promise).resolves(object);358 return stub().then(function (actual) {359 assert.same(actual, object, "Same object resolved");360 assert.isTrue(promise.resolve.calledOnce, "Custom promise resolve called once");361 assert.isTrue(promise.resolve.calledWith(object), "Custom promise resolve called once with expected");362 });363 });364 it("should set the promise used by reject", function () {365 var stub = createStub.create();366 var promise = {367 reject: createStub.create().callsFake(function (err) {368 return Promise.reject(err);369 })370 };371 var reason = new Error();372 stub.usingPromise(promise).rejects(reason);373 return stub().then(function () {374 referee.fail("this should not resolve");375 }).catch(function (actual) {376 assert.same(actual, reason, "Same object resolved");377 assert.isTrue(promise.reject.calledOnce, "Custom promise reject called once");378 assert.isTrue(promise.reject.calledWith(reason), "Custom promise reject called once with expected");379 });380 });381 });382 describe(".throws", function () {383 it("throws specified exception", function () {384 var stub = createStub.create();385 var error = new Error();386 stub.throws(error);387 assert.exception(stub, error);388 });389 it("returns stub", function () {390 var stub = createStub.create();391 assert.same(stub.throws({}), stub);392 });393 it("sets type of exception to throw", function () {394 var stub = createStub.create();395 var exceptionType = "TypeError";396 stub.throws(exceptionType);397 assert.exception(function () {398 stub();399 }, exceptionType);400 });401 it("specifies exception message", function () {402 var stub = createStub.create();403 var message = "Oh no!";404 stub.throws("Error", message);405 assert.exception(stub, {406 message: message407 });408 });409 it("does not specify exception message if not provided", function () {410 var stub = createStub.create();411 stub.throws("Error");412 assert.exception(stub, {413 message: ""414 });415 });416 it("throws generic error", function () {417 var stub = createStub.create();418 stub.throws();419 assert.exception(stub, "Error");420 });421 it("resets 'invoking' flag", function () {422 var stub = createStub.create();423 stub.throws();424 assert.exception(stub);425 refute.defined(stub.invoking);426 });427 });428 describe(".callsArg", function () {429 beforeEach(function () {430 this.stub = createStub.create();431 });432 it("calls argument at specified index", function () {433 this.stub.callsArg(2);434 var callback = createStub.create();435 this.stub(1, 2, callback);436 assert(callback.called);437 });438 it("returns stub", function () {439 assert.isFunction(this.stub.callsArg(2));440 });441 it("throws if argument at specified index is not callable", function () {442 this.stub.callsArg(0);443 assert.exception(function () {444 this.stub(1);445 }, "TypeError");446 });447 it("throws if no index is specified", function () {448 var stub = this.stub;449 assert.exception(function () {450 stub.callsArg();451 }, "TypeError");452 });453 it("throws if index is not number", function () {454 var stub = this.stub;455 assert.exception(function () {456 stub.callsArg({});457 }, "TypeError");458 });459 });460 describe(".callsArgWith", function () {461 beforeEach(function () {462 this.stub = createStub.create();463 });464 it("calls argument at specified index with provided args", function () {465 var object = {};466 this.stub.callsArgWith(1, object);467 var callback = createStub.create();468 this.stub(1, callback);469 assert(callback.calledWith(object));470 });471 it("returns function", function () {472 var stub = this.stub.callsArgWith(2, 3);473 assert.isFunction(stub);474 });475 it("calls callback without args", function () {476 this.stub.callsArgWith(1);477 var callback = createStub.create();478 this.stub(1, callback);479 assert(callback.calledWith());480 });481 it("calls callback with multiple args", function () {482 var object = {};483 var array = [];484 this.stub.callsArgWith(1, object, array);485 var callback = createStub.create();486 this.stub(1, callback);487 assert(callback.calledWith(object, array));488 });489 it("throws if no index is specified", function () {490 var stub = this.stub;491 assert.exception(function () {492 stub.callsArgWith();493 }, "TypeError");494 });495 it("throws if index is not number", function () {496 var stub = this.stub;497 assert.exception(function () {498 stub.callsArgWith({});499 }, "TypeError");500 });501 });502 describe(".callsArgOn", function () {503 beforeEach(function () {504 this.stub = createStub.create();505 this.fakeContext = {506 foo: "bar"507 };508 });509 it("calls argument at specified index", function () {510 this.stub.callsArgOn(2, this.fakeContext);511 var callback = createStub.create();512 this.stub(1, 2, callback);513 assert(callback.called);514 assert(callback.calledOn(this.fakeContext));515 });516 it("calls argument at specified index with undefined context", function () {517 this.stub.callsArgOn(2, undefined);518 var callback = createStub.create();519 this.stub(1, 2, callback);520 assert(callback.called);521 assert(callback.calledOn(undefined));522 });523 it("calls argument at specified index with number context", function () {524 this.stub.callsArgOn(2, 5);525 var callback = createStub.create();526 this.stub(1, 2, callback);527 assert(callback.called);528 assert(callback.calledOn(5));529 });530 it("returns stub", function () {531 var stub = this.stub.callsArgOn(2, this.fakeContext);532 assert.isFunction(stub);533 });534 it("throws if argument at specified index is not callable", function () {535 this.stub.callsArgOn(0, this.fakeContext);536 assert.exception(function () {537 this.stub(1);538 }, "TypeError");539 });540 it("throws if no index is specified", function () {541 var stub = this.stub;542 assert.exception(function () {543 stub.callsArgOn();544 }, "TypeError");545 });546 it("throws if index is not number", function () {547 var stub = this.stub;548 assert.exception(function () {549 stub.callsArgOn(this.fakeContext, 2);550 }, "TypeError");551 });552 });553 describe(".callsArgOnWith", function () {554 beforeEach(function () {555 this.stub = createStub.create();556 this.fakeContext = { foo: "bar" };557 });558 it("calls argument at specified index with provided args", function () {559 var object = {};560 this.stub.callsArgOnWith(1, this.fakeContext, object);561 var callback = createStub.create();562 this.stub(1, callback);563 assert(callback.calledWith(object));564 assert(callback.calledOn(this.fakeContext));565 });566 it("calls argument at specified index with provided args and undefined context", function () {567 var object = {};568 this.stub.callsArgOnWith(1, undefined, object);569 var callback = createStub.create();570 this.stub(1, callback);571 assert(callback.calledWith(object));572 assert(callback.calledOn(undefined));573 });574 it("calls argument at specified index with provided args and number context", function () {575 var object = {};576 this.stub.callsArgOnWith(1, 5, object);577 var callback = createStub.create();578 this.stub(1, callback);579 assert(callback.calledWith(object));580 assert(callback.calledOn(5));581 });582 it("calls argument at specified index with provided args with undefined context", function () {583 var object = {};584 this.stub.callsArgOnWith(1, undefined, object);585 var callback = createStub.create();586 this.stub(1, callback);587 assert(callback.calledWith(object));588 assert(callback.calledOn(undefined));589 });590 it("calls argument at specified index with provided args with number context", function () {591 var object = {};592 this.stub.callsArgOnWith(1, 5, object);593 var callback = createStub.create();594 this.stub(1, callback);595 assert(callback.calledWith(object));596 assert(callback.calledOn(5));597 });598 it("returns function", function () {599 var stub = this.stub.callsArgOnWith(2, this.fakeContext, 3);600 assert.isFunction(stub);601 });602 it("calls callback without args", function () {603 this.stub.callsArgOnWith(1, this.fakeContext);604 var callback = createStub.create();605 this.stub(1, callback);606 assert(callback.calledWith());607 assert(callback.calledOn(this.fakeContext));608 });609 it("calls callback with multiple args", function () {610 var object = {};611 var array = [];612 this.stub.callsArgOnWith(1, this.fakeContext, object, array);613 var callback = createStub.create();614 this.stub(1, callback);615 assert(callback.calledWith(object, array));616 assert(callback.calledOn(this.fakeContext));617 });618 it("throws if no index is specified", function () {619 var stub = this.stub;620 assert.exception(function () {621 stub.callsArgOnWith();622 }, "TypeError");623 });624 it("throws if index is not number", function () {625 var stub = this.stub;626 assert.exception(function () {627 stub.callsArgOnWith({});628 }, "TypeError");629 });630 });631 describe(".callsFake", function () {632 beforeEach(function () {633 this.method = function () { throw new Error("Should be stubbed"); };634 this.object = {method: this.method};635 });636 it("uses provided function as stub", function () {637 var fakeFn = createStub.create();638 this.stub = createStub(this.object, "method");639 this.stub.callsFake(fakeFn);640 this.object.method(1, 2);641 assert(fakeFn.calledWith(1, 2));642 assert(fakeFn.calledOn(this.object));643 });644 it("is overwritten by subsequent stub behavior", function () {645 var fakeFn = createStub.create();646 this.stub = createStub(this.object, "method");647 this.stub.callsFake(fakeFn).returns(3);648 var returned = this.object.method(1, 2);649 refute(fakeFn.called);650 assert(returned === 3);651 });652 });653 describe(".objectMethod", function () {654 beforeEach(function () {655 this.method = function () {};656 this.object = { method: this.method };657 });658 afterEach(function () {659 if (global.console.info.restore) {660 global.console.info.restore();661 }662 });663 it("warns provided function as stub, recommending callsFake instead", function () {664 deprecated.printWarning.restore();665 var called = false;666 var infoStub = createStub(console, "info");667 var stub = createStub(this.object, "method", function () {668 called = true;669 });670 stub();671 assert(called);672 assert(infoStub.called);673 });674 it("throws if third argument is provided but not a proprety descriptor", function () {675 var object = this.object;676 assert.exception(function () {677 createStub(object, "method", 1);678 }, "TypeError");679 });680 it("stubbed method should be proper stub", function () {681 var stub = createStub(this.object, "method");682 assert.isFunction(stub.returns);683 assert.isFunction(stub.throws);684 });685 it("stub should be spy", function () {686 var stub = createStub(this.object, "method");687 this.object.method();688 assert(stub.called);689 assert(stub.calledOn(this.object));690 });691 it("stub should affect spy", function () {692 var stub = createStub(this.object, "method");693 stub.throws("TypeError");694 assert.exception(this.object.method);695 assert(stub.threw("TypeError"));696 });697 it("returns standalone stub without arguments", function () {698 var stub = createStub();699 assert.isFunction(stub);700 assert.isFalse(stub.called);701 });702 it("successfully stubs falsey properties", function () {703 var obj = { 0: function () { } };704 createStub(obj, 0).callsFake(function () {705 return "stubbed value";706 });707 assert.equals(obj[0](), "stubbed value");708 });709 it("does not stub function object", function () {710 assert.exception(function () {711 createStub(function () {});712 });713 });714 });715 describe("everything", function () {716 it("stubs all methods of object without property", function () {717 var obj = {718 func1: function () {},719 func2: function () {},720 func3: function () {}721 };722 createStub(obj);723 assert.isFunction(obj.func1.restore);724 assert.isFunction(obj.func2.restore);725 assert.isFunction(obj.func3.restore);726 });727 it("stubs prototype methods", function () {728 function Obj() {}729 Obj.prototype.func1 = function () {};730 var obj = new Obj();731 createStub(obj);732 assert.isFunction(obj.func1.restore);733 });734 it("returns object", function () {735 var object = {};736 assert.same(createStub(object), object);737 });738 it("only stubs functions", function () {739 var object = { foo: "bar" };740 createStub(object);741 assert.equals(object.foo, "bar");742 });743 it("handles non-enumerable properties", function () {744 var obj = {745 func1: function () {},746 func2: function () {}747 };748 Object.defineProperty(obj, "func3", {749 value: function () {},750 writable: true,751 configurable: true752 });753 createStub(obj);754 assert.isFunction(obj.func1.restore);755 assert.isFunction(obj.func2.restore);756 assert.isFunction(obj.func3.restore);757 });758 it("handles non-enumerable properties on prototypes", function () {759 function Obj() {}760 Object.defineProperty(Obj.prototype, "func1", {761 value: function () {},762 writable: true,763 configurable: true764 });765 var obj = new Obj();766 createStub(obj);767 assert.isFunction(obj.func1.restore);768 });769 it("does not stub non-enumerable properties from Object.prototype", function () {770 var obj = {};771 createStub(obj);772 refute.isFunction(obj.toString.restore);773 refute.isFunction(obj.toLocaleString.restore);774 refute.isFunction(obj.propertyIsEnumerable.restore);775 });776 it("does not fail on overrides", function () {777 var parent = {778 func: function () {}779 };780 var child = Object.create(parent);781 child.func = function () {};782 refute.exception(function () {783 createStub(child);784 });785 });786 it("does not call getter during restore", function () {787 var obj = {788 get prop() {789 fail("should not call getter");790 }791 };792 var stub = createStub(obj, "prop", {get: function () {793 return 43;794 }});795 assert.equals(obj.prop, 43);796 stub.restore();797 });798 });799 describe("stubbed function", function () {800 it("has toString method", function () {801 var obj = { meth: function () {} };802 createStub(obj, "meth");803 assert.equals(obj.meth.toString(), "meth");804 });805 it("toString should say 'stub' when unable to infer name", function () {806 var stub = createStub();807 assert.equals(stub.toString(), "stub");808 });809 it("toString should prefer property name if possible", function () {810 var obj = {};811 obj.meth = createStub();812 obj.meth();813 assert.equals(obj.meth.toString(), "meth");814 });815 });816 describe(".yields", function () {817 it("invokes only argument as callback", function () {818 var stub = createStub().yields();819 var spy = createSpy();820 stub(spy);821 assert(spy.calledOnce);822 assert.equals(spy.args[0].length, 0);823 });824 it("throws understandable error if no callback is passed", function () {825 var stub = createStub().yields();826 assert.exception(stub, {827 message: "stub expected to yield, but no callback was passed."828 });829 });830 it("includes stub name and actual arguments in error", function () {831 var myObj = { somethingAwesome: function () {} };832 var stub = createStub(myObj, "somethingAwesome").yields();833 assert.exception(834 function () {835 stub(23, 42);836 },837 {838 message: "somethingAwesome expected to yield, but no callback " +839 "was passed. Received [23, 42]"840 }841 );842 });843 it("invokes last argument as callback", function () {844 var stub = createStub().yields();845 var spy = createSpy();846 stub(24, {}, spy);847 assert(spy.calledOnce);848 assert.equals(spy.args[0].length, 0);849 });850 it("invokes first of two callbacks", function () {851 var stub = createStub().yields();852 var spy = createSpy();853 var spy2 = createSpy();854 stub(24, {}, spy, spy2);855 assert(spy.calledOnce);856 assert(!spy2.called);857 });858 it("invokes callback with arguments", function () {859 var obj = { id: 42 };860 var stub = createStub().yields(obj, "Crazy");861 var spy = createSpy();862 stub(spy);863 assert(spy.calledWith(obj, "Crazy"));864 });865 it("throws if callback throws", function () {866 var obj = { id: 42 };867 var stub = createStub().yields(obj, "Crazy");868 var callback = createStub().throws();869 assert.exception(function () {870 stub(callback);871 });872 });873 it("plays nice with throws", function () {874 var stub = createStub().throws().yields();875 var spy = createSpy();876 assert.exception(function () {877 stub(spy);878 });879 assert(spy.calledOnce);880 });881 it("plays nice with returns", function () {882 var obj = {};883 var stub = createStub().returns(obj).yields();884 var spy = createSpy();885 assert.same(stub(spy), obj);886 assert(spy.calledOnce);887 });888 it("plays nice with returnsArg", function () {889 var stub = createStub().returnsArg(0).yields();890 var spy = createSpy();891 assert.same(stub(spy), spy);892 assert(spy.calledOnce);893 });894 it("plays nice with returnsThis", function () {895 var obj = {};896 var stub = createStub().returnsThis().yields();897 var spy = createSpy();898 assert.same(stub.call(obj, spy), obj);899 assert(spy.calledOnce);900 });901 });902 describe(".yieldsRight", function () {903 it("invokes only argument as callback", function () {904 var stub = createStub().yieldsRight();905 var spy = createSpy();906 stub(spy);907 assert(spy.calledOnce);908 assert.equals(spy.args[0].length, 0);909 });910 it("throws understandable error if no callback is passed", function () {911 var stub = createStub().yieldsRight();912 assert.exception(stub, {913 message: "stub expected to yield, but no callback was passed."914 });915 });916 it("includes stub name and actual arguments in error", function () {917 var myObj = { somethingAwesome: function () {} };918 var stub = createStub(myObj, "somethingAwesome").yieldsRight();919 assert.exception(920 function () {921 stub(23, 42);922 },923 {924 message: "somethingAwesome expected to yield, but no callback " +925 "was passed. Received [23, 42]"926 }927 );928 });929 it("invokes last argument as callback", function () {930 var stub = createStub().yieldsRight();931 var spy = createSpy();932 stub(24, {}, spy);933 assert(spy.calledOnce);934 assert.equals(spy.args[0].length, 0);935 });936 it("invokes the last of two callbacks", function () {937 var stub = createStub().yieldsRight();938 var spy = createSpy();939 var spy2 = createSpy();940 stub(24, {}, spy, spy2);941 assert(!spy.called);942 assert(spy2.calledOnce);943 });944 it("invokes callback with arguments", function () {945 var obj = { id: 42 };946 var stub = createStub().yieldsRight(obj, "Crazy");947 var spy = createSpy();948 stub(spy);949 assert(spy.calledWith(obj, "Crazy"));950 });951 it("throws if callback throws", function () {952 var obj = { id: 42 };953 var stub = createStub().yieldsRight(obj, "Crazy");954 var callback = createStub().throws();955 assert.exception(function () {956 stub(callback);957 });958 });959 it("plays nice with throws", function () {960 var stub = createStub().throws().yieldsRight();961 var spy = createSpy();962 assert.exception(function () {963 stub(spy);964 });965 assert(spy.calledOnce);966 });967 it("plays nice with returns", function () {968 var obj = {};969 var stub = createStub().returns(obj).yieldsRight();970 var spy = createSpy();971 assert.same(stub(spy), obj);972 assert(spy.calledOnce);973 });974 it("plays nice with returnsArg", function () {975 var stub = createStub().returnsArg(0).yieldsRight();976 var spy = createSpy();977 assert.same(stub(spy), spy);978 assert(spy.calledOnce);979 });980 it("plays nice with returnsThis", function () {981 var obj = {};982 var stub = createStub().returnsThis().yieldsRight();983 var spy = createSpy();984 assert.same(stub.call(obj, spy), obj);985 assert(spy.calledOnce);986 });987 });988 describe(".yieldsOn", function () {989 beforeEach(function () {990 this.stub = createStub.create();991 this.fakeContext = { foo: "bar" };992 });993 it("invokes only argument as callback", function () {994 var spy = createSpy();995 this.stub.yieldsOn(this.fakeContext);996 this.stub(spy);997 assert(spy.calledOnce);998 assert(spy.calledOn(this.fakeContext));999 assert.equals(spy.args[0].length, 0);1000 });1001 it("throws if no context is specified", function () {1002 assert.exception(function () {1003 this.stub.yieldsOn();1004 }, "TypeError");1005 });1006 it("throws understandable error if no callback is passed", function () {1007 this.stub.yieldsOn(this.fakeContext);1008 assert.exception(this.stub, {1009 message: "stub expected to yield, but no callback was passed."1010 });1011 });1012 it("includes stub name and actual arguments in error", function () {1013 var myObj = { somethingAwesome: function () {} };1014 var stub = createStub(myObj, "somethingAwesome").yieldsOn(this.fakeContext);1015 assert.exception(1016 function () {1017 stub(23, 42);1018 },1019 {1020 message: "somethingAwesome expected to yield, but no callback " +1021 "was passed. Received [23, 42]"1022 }1023 );1024 });1025 it("invokes last argument as callback", function () {1026 var spy = createSpy();1027 this.stub.yieldsOn(this.fakeContext);1028 this.stub(24, {}, spy);1029 assert(spy.calledOnce);1030 assert(spy.calledOn(this.fakeContext));1031 assert.equals(spy.args[0].length, 0);1032 });1033 it("invokes first of two callbacks", function () {1034 var spy = createSpy();1035 var spy2 = createSpy();1036 this.stub.yieldsOn(this.fakeContext);1037 this.stub(24, {}, spy, spy2);1038 assert(spy.calledOnce);1039 assert(spy.calledOn(this.fakeContext));1040 assert(!spy2.called);1041 });1042 it("invokes callback with arguments", function () {1043 var obj = { id: 42 };1044 var spy = createSpy();1045 this.stub.yieldsOn(this.fakeContext, obj, "Crazy");1046 this.stub(spy);1047 assert(spy.calledWith(obj, "Crazy"));1048 assert(spy.calledOn(this.fakeContext));1049 });1050 it("throws if callback throws", function () {1051 var obj = { id: 42 };1052 var callback = createStub().throws();1053 this.stub.yieldsOn(this.fakeContext, obj, "Crazy");1054 assert.exception(function () {1055 this.stub(callback);1056 });1057 });1058 });1059 describe(".yieldsTo", function () {1060 it("yields to property of object argument", function () {1061 var stub = createStub().yieldsTo("success");1062 var callback = createSpy();1063 stub({ success: callback });1064 assert(callback.calledOnce);1065 assert.equals(callback.args[0].length, 0);1066 });1067 it("throws understandable error if no object with callback is passed", function () {1068 var stub = createStub().yieldsTo("success");1069 assert.exception(stub, {1070 message: "stub expected to yield to 'success', but no object " +1071 "with such a property was passed."1072 });1073 });1074 it("throws understandable error if failing to yield callback by symbol", function () {1075 if (typeof Symbol === "function") {1076 var symbol = Symbol();1077 var stub = createStub().yieldsTo(symbol);1078 assert.exception(stub, {1079 message: "stub expected to yield to 'Symbol()', but no object with " +1080 "such a property was passed."1081 });1082 }1083 });1084 it("includes stub name and actual arguments in error", function () {1085 var myObj = { somethingAwesome: function () {} };1086 var stub = createStub(myObj, "somethingAwesome").yieldsTo("success");1087 assert.exception(1088 function () {1089 stub(23, 42);1090 },1091 {1092 message: "somethingAwesome expected to yield to 'success', but " +1093 "no object with such a property was passed. " +1094 "Received [23, 42]"1095 }1096 );1097 });1098 it("invokes property on last argument as callback", function () {1099 var stub = createStub().yieldsTo("success");1100 var callback = createSpy();1101 stub(24, {}, { success: callback });1102 assert(callback.calledOnce);1103 assert.equals(callback.args[0].length, 0);1104 });1105 it("invokes first of two possible callbacks", function () {1106 var stub = createStub().yieldsTo("error");1107 var callback = createSpy();1108 var callback2 = createSpy();1109 stub(24, {}, { error: callback }, { error: callback2 });1110 assert(callback.calledOnce);1111 assert(!callback2.called);1112 });1113 it("invokes callback with arguments", function () {1114 var obj = { id: 42 };1115 var stub = createStub().yieldsTo("success", obj, "Crazy");1116 var callback = createSpy();1117 stub({ success: callback });1118 assert(callback.calledWith(obj, "Crazy"));1119 });1120 it("throws if callback throws", function () {1121 var obj = { id: 42 };1122 var stub = createStub().yieldsTo("error", obj, "Crazy");1123 var callback = createStub().throws();1124 assert.exception(function () {1125 stub({ error: callback });1126 });1127 });1128 });1129 describe(".yieldsToOn", function () {1130 beforeEach(function () {1131 this.stub = createStub.create();1132 this.fakeContext = { foo: "bar" };1133 });1134 it("yields to property of object argument", function () {1135 this.stub.yieldsToOn("success", this.fakeContext);1136 var callback = createSpy();1137 this.stub({ success: callback });1138 assert(callback.calledOnce);1139 assert(callback.calledOn(this.fakeContext));1140 assert.equals(callback.args[0].length, 0);1141 });1142 it("yields to property of object argument with undefined context", function () {1143 this.stub.yieldsToOn("success", undefined);1144 var callback = createSpy();1145 this.stub({ success: callback });1146 assert(callback.calledOnce);1147 assert(callback.calledOn(undefined));1148 assert.equals(callback.args[0].length, 0);1149 });1150 it("yields to property of object argument with number context", function () {1151 this.stub.yieldsToOn("success", 5);1152 var callback = createSpy();1153 this.stub({ success: callback });1154 assert(callback.calledOnce);1155 assert(callback.calledOn(5));1156 assert.equals(callback.args[0].length, 0);1157 });1158 it("throws understandable error if no object with callback is passed", function () {1159 this.stub.yieldsToOn("success", this.fakeContext);1160 assert.exception(this.stub, {1161 message: "stub expected to yield to 'success', but no object " +1162 "with such a property was passed."1163 });1164 });1165 it("includes stub name and actual arguments in error", function () {1166 var myObj = { somethingAwesome: function () {} };1167 var stub = createStub(myObj, "somethingAwesome").yieldsToOn("success", this.fakeContext);1168 assert.exception(1169 function () {1170 stub(23, 42);1171 },1172 {1173 message: "somethingAwesome expected to yield to 'success', but " +1174 "no object with such a property was passed. " +1175 "Received [23, 42]"1176 }1177 );1178 });1179 it("invokes property on last argument as callback", function () {1180 var callback = createSpy();1181 this.stub.yieldsToOn("success", this.fakeContext);1182 this.stub(24, {}, { success: callback });1183 assert(callback.calledOnce);1184 assert(callback.calledOn(this.fakeContext));1185 assert.equals(callback.args[0].length, 0);1186 });1187 it("invokes first of two possible callbacks", function () {1188 var callback = createSpy();1189 var callback2 = createSpy();1190 this.stub.yieldsToOn("error", this.fakeContext);1191 this.stub(24, {}, { error: callback }, { error: callback2 });1192 assert(callback.calledOnce);1193 assert(callback.calledOn(this.fakeContext));1194 assert(!callback2.called);1195 });1196 it("invokes callback with arguments", function () {1197 var obj = { id: 42 };1198 var callback = createSpy();1199 this.stub.yieldsToOn("success", this.fakeContext, obj, "Crazy");1200 this.stub({ success: callback });1201 assert(callback.calledOn(this.fakeContext));1202 assert(callback.calledWith(obj, "Crazy"));1203 });1204 it("throws if callback throws", function () {1205 var obj = { id: 42 };1206 var callback = createStub().throws();1207 this.stub.yieldsToOn("error", this.fakeContext, obj, "Crazy");1208 assert.exception(function () {1209 this.stub({ error: callback });1210 });1211 });1212 });1213 describe(".withArgs", function () {1214 it("defines withArgs method", function () {1215 var stub = createStub();1216 assert.isFunction(stub.withArgs);1217 });1218 it("creates filtered stub", function () {1219 var stub = createStub();1220 var other = stub.withArgs(23);1221 refute.same(other, stub);1222 assert.isFunction(stub.returns);1223 assert.isFunction(other.returns);1224 });1225 it("filters return values based on arguments", function () {1226 var stub = createStub().returns(23);1227 stub.withArgs(42).returns(99);1228 assert.equals(stub(), 23);1229 assert.equals(stub(42), 99);1230 });1231 it("filters exceptions based on arguments", function () {1232 var stub = createStub().returns(23);1233 stub.withArgs(42).throws();1234 refute.exception(stub);1235 assert.exception(function () {1236 stub(42);1237 });1238 });1239 it("ensure stub recognizes sinonMatch fuzzy arguments", function () {1240 var stub = createStub().returns(23);1241 stub.withArgs(sinonMatch({ foo: "bar" })).returns(99);1242 assert.equals(stub(), 23);1243 assert.equals(stub({ foo: "bar", bar: "foo" }), 99);1244 });1245 it("ensure stub uses last matching arguments", function () {1246 var unmatchedValue = "d3ada6a0-8dac-4136-956d-033b5f23eadf";1247 var firstMatchedValue = "68128619-a639-4b32-a4a0-6519165bf301";1248 var secondMatchedValue = "4ac2dc8f-3f3f-4648-9838-a2825fd94c9a";1249 var expectedArgument = "3e1ed1ec-c377-4432-a33e-3c937f1406d1";1250 var stub = createStub().returns(unmatchedValue);1251 stub.withArgs(expectedArgument).returns(firstMatchedValue);1252 stub.withArgs(expectedArgument).returns(secondMatchedValue);1253 assert.equals(stub(), unmatchedValue);1254 assert.equals(stub(expectedArgument), secondMatchedValue);1255 });1256 it("ensure stub uses last matching sinonMatch arguments", function () {1257 var unmatchedValue = "0aa66a7d-3c50-49ef-8365-bdcab637b2dd";1258 var firstMatchedValue = "1ab2c601-7602-4658-9377-3346f6814caa";1259 var secondMatchedValue = "e2e31518-c4c4-4012-a61f-31942f603ffa";1260 var expectedArgument = "90dc4a22-ef53-4c62-8e05-4cf4b4bf42fa";1261 var stub = createStub().returns(unmatchedValue);1262 stub.withArgs(expectedArgument).returns(firstMatchedValue);1263 stub.withArgs(sinonMatch(expectedArgument)).returns(secondMatchedValue);1264 assert.equals(stub(), unmatchedValue);1265 assert.equals(stub(expectedArgument), secondMatchedValue);1266 });1267 });1268 describe(".callsArgAsync", function () {1269 beforeEach(function () {1270 this.stub = createStub.create();1271 });1272 it("asynchronously calls argument at specified index", function (done) {1273 this.stub.callsArgAsync(2);1274 var callback = createSpy(done);1275 this.stub(1, 2, callback);1276 assert(!callback.called);1277 });1278 });1279 describe(".callsArgWithAsync", function () {1280 beforeEach(function () {1281 this.stub = createStub.create();1282 });1283 it("asynchronously calls callback at specified index with multiple args", function (done) {1284 var object = {};1285 var array = [];1286 this.stub.callsArgWithAsync(1, object, array);1287 var callback = createSpy(function () {1288 assert(callback.calledWith(object, array));1289 done();1290 });1291 this.stub(1, callback);1292 assert(!callback.called);1293 });1294 });1295 describe(".callsArgOnAsync", function () {1296 beforeEach(function () {1297 this.stub = createStub.create();1298 this.fakeContext = {1299 foo: "bar"1300 };1301 });1302 it("asynchronously calls argument at specified index with specified context", function (done) {1303 var context = this.fakeContext;1304 this.stub.callsArgOnAsync(2, context);1305 var callback = createSpy(function () {1306 assert(callback.calledOn(context));1307 done();1308 });1309 this.stub(1, 2, callback);1310 assert(!callback.called);1311 });1312 });1313 describe(".callsArgOnWithAsync", function () {1314 beforeEach(function () {1315 this.stub = createStub.create();1316 this.fakeContext = { foo: "bar" };1317 });1318 it("asynchronously calls argument at specified index with provided context and args", function (done) {1319 var object = {};1320 var context = this.fakeContext;1321 this.stub.callsArgOnWithAsync(1, context, object);1322 var callback = createSpy(function () {1323 assert(callback.calledOn(context));1324 assert(callback.calledWith(object));1325 done();1326 });1327 this.stub(1, callback);1328 assert(!callback.called);1329 });1330 });1331 describe(".yieldsAsync", function () {1332 it("asynchronously invokes only argument as callback", function (done) {1333 var stub = createStub().yieldsAsync();1334 var spy = createSpy(done);1335 stub(spy);1336 assert(!spy.called);1337 });1338 });1339 describe(".yieldsOnAsync", function () {1340 beforeEach(function () {1341 this.stub = createStub.create();1342 this.fakeContext = { foo: "bar" };1343 });1344 it("asynchronously invokes only argument as callback with given context", function (done) {1345 var context = this.fakeContext;1346 this.stub.yieldsOnAsync(context);1347 var spy = createSpy(function () {1348 assert(spy.calledOnce);1349 assert(spy.calledOn(context));1350 assert.equals(spy.args[0].length, 0);1351 done();1352 });1353 this.stub(spy);1354 assert(!spy.called);1355 });1356 });1357 describe(".yieldsToAsync", function () {1358 it("asynchronously yields to property of object argument", function (done) {1359 var stub = createStub().yieldsToAsync("success");1360 var callback = createSpy(function () {1361 assert(callback.calledOnce);1362 assert.equals(callback.args[0].length, 0);1363 done();1364 });1365 stub({ success: callback });1366 assert(!callback.called);1367 });1368 });1369 describe(".yieldsToOnAsync", function () {1370 beforeEach(function () {1371 this.stub = createStub.create();1372 this.fakeContext = { foo: "bar" };1373 });1374 it("asynchronously yields to property of object argument with given context", function (done) {1375 var context = this.fakeContext;1376 this.stub.yieldsToOnAsync("success", context);1377 var callback = createSpy(function () {1378 assert(callback.calledOnce);1379 assert(callback.calledOn(context));1380 assert.equals(callback.args[0].length, 0);1381 done();1382 });1383 this.stub({ success: callback });1384 assert(!callback.called);1385 });1386 });1387 describe(".onCall", function () {1388 it("can be used with returns to produce sequence", function () {1389 var stub = createStub().returns(3);1390 stub.onFirstCall().returns(1)1391 .onCall(2).returns(2);1392 assert.same(stub(), 1);1393 assert.same(stub(), 3);1394 assert.same(stub(), 2);1395 assert.same(stub(), 3);1396 });1397 it("can be used with returnsArg to produce sequence", function () {1398 var stub = createStub().returns("default");1399 stub.onSecondCall().returnsArg(0);1400 assert.same(stub(1), "default");1401 assert.same(stub(2), 2);1402 assert.same(stub(3), "default");1403 });1404 it("can be used with returnsThis to produce sequence", function () {1405 var instance = {};1406 instance.stub = createStub().returns("default");1407 instance.stub.onSecondCall().returnsThis();1408 assert.same(instance.stub(), "default");1409 assert.same(instance.stub(), instance);1410 assert.same(instance.stub(), "default");1411 });1412 it("can be used with throwsException to produce sequence", function () {1413 var stub = createStub();1414 var error = new Error();1415 stub.onSecondCall().throwsException(error);1416 stub();1417 assert.exception(stub, function (e) {1418 return e === error;1419 });1420 });1421 it("supports chained declaration of behavior", function () {1422 var stub = createStub()1423 .onCall(0).returns(1)1424 .onCall(1).returns(2)1425 .onCall(2).returns(3);1426 assert.same(stub(), 1);1427 assert.same(stub(), 2);1428 assert.same(stub(), 3);1429 });1430 describe("in combination with withArgs", function () {1431 it("can produce a sequence for a fake", function () {1432 var stub = createStub().returns(0);1433 stub.withArgs(5).returns(-1)1434 .onFirstCall().returns(1)1435 .onSecondCall().returns(2);1436 assert.same(stub(0), 0);1437 assert.same(stub(5), 1);1438 assert.same(stub(0), 0);1439 assert.same(stub(5), 2);1440 assert.same(stub(5), -1);1441 });1442 it("falls back to stub default behaviour if fake does not have its own default behaviour", function () {1443 var stub = createStub().returns(0);1444 stub.withArgs(5)1445 .onFirstCall().returns(1);1446 assert.same(stub(5), 1);1447 assert.same(stub(5), 0);1448 });1449 it("falls back to stub behaviour for call if fake does not have its own behaviour for call", function () {1450 var stub = createStub().returns(0);1451 stub.withArgs(5).onFirstCall().returns(1);1452 stub.onSecondCall().returns(2);1453 assert.same(stub(5), 1);1454 assert.same(stub(5), 2);1455 assert.same(stub(4), 0);1456 });1457 it("defaults to undefined behaviour once no more calls have been defined", function () {1458 var stub = createStub();1459 stub.withArgs(5).onFirstCall().returns(1)1460 .onSecondCall().returns(2);1461 assert.same(stub(5), 1);1462 assert.same(stub(5), 2);1463 refute.defined(stub(5));1464 });1465 it("does not create undefined behaviour just by calling onCall", function () {1466 var stub = createStub().returns(2);1467 stub.onFirstCall();1468 assert.same(stub(6), 2);1469 });1470 it("works with fakes and reset", function () {1471 var stub = createStub();1472 stub.withArgs(5).onFirstCall().returns(1);1473 stub.withArgs(5).onSecondCall().returns(2);1474 assert.same(stub(5), 1);1475 assert.same(stub(5), 2);1476 refute.defined(stub(5));1477 stub.reset();1478 assert.same(stub(5), undefined);1479 assert.same(stub(5), undefined);1480 refute.defined(stub(5));1481 });1482 it("throws an understandable error when trying to use withArgs on behavior", function () {1483 assert.exception(1484 function () {1485 createStub().onFirstCall().withArgs(1);1486 },1487 {1488 message: /not supported/1489 }1490 );1491 });1492 });1493 it("can be used with yields* to produce a sequence", function () {1494 var context = { foo: "bar" };1495 var obj = { method1: createSpy(), method2: createSpy() };1496 var obj2 = { method2: createSpy() };1497 var stub = createStub().yieldsToOn("method2", context, 7, 8);1498 stub.onFirstCall().yields(1, 2)1499 .onSecondCall().yieldsOn(context, 3, 4)1500 .onThirdCall().yieldsTo("method1", 5, 6)1501 .onCall(3).yieldsToOn("method2", context, 7, 8);1502 var spy1 = createSpy();1503 var spy2 = createSpy();1504 stub(spy1);1505 stub(spy2);1506 stub(obj);1507 stub(obj);1508 stub(obj2); // should continue with default behavior1509 assert(spy1.calledOnce);1510 assert(spy1.calledWithExactly(1, 2));1511 assert(spy2.calledOnce);1512 assert(spy2.calledAfter(spy1));1513 assert(spy2.calledOn(context));1514 assert(spy2.calledWithExactly(3, 4));1515 assert(obj.method1.calledOnce);1516 assert(obj.method1.calledAfter(spy2));1517 assert(obj.method1.calledWithExactly(5, 6));1518 assert(obj.method2.calledOnce);1519 assert(obj.method2.calledAfter(obj.method1));1520 assert(obj.method2.calledOn(context));1521 assert(obj.method2.calledWithExactly(7, 8));1522 assert(obj2.method2.calledOnce);1523 assert(obj2.method2.calledAfter(obj.method2));1524 assert(obj2.method2.calledOn(context));1525 assert(obj2.method2.calledWithExactly(7, 8));1526 });1527 it("can be used with callsArg* to produce a sequence", function () {1528 var spy1 = createSpy();1529 var spy2 = createSpy();1530 var spy3 = createSpy();1531 var spy4 = createSpy();1532 var spy5 = createSpy();1533 var decoy = createSpy();1534 var context = { foo: "bar" };1535 var stub = createStub().callsArgOnWith(3, context, "c", "d");1536 stub.onFirstCall().callsArg(0)1537 .onSecondCall().callsArgWith(1, "a", "b")1538 .onThirdCall().callsArgOn(2, context)1539 .onCall(3).callsArgOnWith(3, context, "c", "d");1540 stub(spy1);1541 stub(decoy, spy2);1542 stub(decoy, decoy, spy3);1543 stub(decoy, decoy, decoy, spy4);1544 stub(decoy, decoy, decoy, spy5); // should continue with default behavior1545 assert(spy1.calledOnce);1546 assert(spy2.calledOnce);1547 assert(spy2.calledAfter(spy1));1548 assert(spy2.calledWithExactly("a", "b"));1549 assert(spy3.calledOnce);1550 assert(spy3.calledAfter(spy2));1551 assert(spy3.calledOn(context));1552 assert(spy4.calledOnce);1553 assert(spy4.calledAfter(spy3));1554 assert(spy4.calledOn(context));1555 assert(spy4.calledWithExactly("c", "d"));1556 assert(spy5.calledOnce);1557 assert(spy5.calledAfter(spy4));1558 assert(spy5.calledOn(context));1559 assert(spy5.calledWithExactly("c", "d"));1560 assert(decoy.notCalled);1561 });1562 it("can be used with yields* and callsArg* in combination to produce a sequence", function () {1563 var stub = createStub().yields(1, 2);1564 stub.onSecondCall().callsArg(1)1565 .onThirdCall().yieldsTo("method")1566 .onCall(3).callsArgWith(2, "a", "b");1567 var obj = { method: createSpy() };1568 var spy1 = createSpy();1569 var spy2 = createSpy();1570 var spy3 = createSpy();1571 var decoy = createSpy();1572 stub(spy1);1573 stub(decoy, spy2);1574 stub(obj);1575 stub(decoy, decoy, spy3);1576 assert(spy1.calledOnce);1577 assert(spy2.calledOnce);1578 assert(spy2.calledAfter(spy1));1579 assert(obj.method.calledOnce);1580 assert(obj.method.calledAfter(spy2));1581 assert(spy3.calledOnce);1582 assert(spy3.calledAfter(obj.method));1583 assert(spy3.calledWithExactly("a", "b"));1584 assert(decoy.notCalled);1585 });1586 it("should interact correctly with assertions (GH-231)", function () {1587 var stub = createStub();1588 var spy = createSpy();1589 stub.callsArgWith(0, "a");1590 stub(spy);1591 assert(spy.calledWith("a"));1592 stub(spy);1593 assert(spy.calledWith("a"));1594 stub.onThirdCall().callsArgWith(0, "b");1595 stub(spy);1596 assert(spy.calledWith("b"));1597 });1598 });1599 describe(".reset", function () {1600 it("resets behavior", function () {1601 var obj = { a: function () {} };1602 var spy = createSpy();1603 createStub(obj, "a").callsArg(1);1604 obj.a(null, spy);1605 obj.a.reset();1606 obj.a(null, spy);1607 assert(spy.calledOnce);1608 });1609 it("resets call history", function () {1610 var stub = createStub();1611 stub(1);1612 stub.reset();1613 stub(2);1614 assert(stub.calledOnce);1615 assert.equals(stub.getCall(0).args[0], 2);1616 });1617 });1618 describe(".resetHistory", function () {1619 it("resets history", function () {1620 var stub = createStub();1621 stub(1);1622 stub.reset();1623 stub(2);1624 assert(stub.calledOnce);1625 assert.equals(stub.getCall(0).args[0], 2);1626 });1627 it("doesn't reset behavior defined using withArgs", function () {1628 var stub = createStub();1629 stub.withArgs("test").returns(10);1630 stub.resetHistory();1631 assert.equals(stub("test"), 10);1632 });1633 it("doesn't reset behavior", function () {1634 var stub = createStub();1635 stub.returns(10);1636 stub.resetHistory();1637 assert.equals(stub("test"), 10);1638 });1639 });1640 describe(".resetBehavior", function () {1641 it("clears yields* and callsArg* sequence", function () {1642 var stub = createStub().yields(1);1643 stub.onFirstCall().callsArg(1);1644 stub.resetBehavior();1645 stub.yields(3);1646 var spyWanted = createSpy();1647 var spyNotWanted = createSpy();1648 stub(spyWanted, spyNotWanted);1649 assert(spyNotWanted.notCalled);1650 assert(spyWanted.calledOnce);1651 assert(spyWanted.calledWithExactly(3));1652 });1653 it("cleans 'returns' behavior", function () {1654 var stub = createStub().returns(1);1655 stub.resetBehavior();1656 refute.defined(stub());1657 });1658 it("cleans behavior of fakes returned by withArgs", function () {1659 var stub = createStub();1660 stub.withArgs("lolz").returns(2);1661 stub.resetBehavior();1662 refute.defined(stub("lolz"));1663 });1664 it("does not clean parents' behavior when called on a fake returned by withArgs", function () {1665 var parentStub = createStub().returns(false);1666 var childStub = parentStub.withArgs("lolz").returns(true);1667 childStub.resetBehavior();1668 assert.same(parentStub("lolz"), false);1669 assert.same(parentStub(), false);1670 });1671 it("cleans 'returnsArg' behavior", function () {1672 var stub = createStub().returnsArg(0);1673 stub.resetBehavior();1674 refute.defined(stub("defined"));1675 });1676 it("cleans 'returnsThis' behavior", function () {1677 var instance = {};1678 instance.stub = createStub.create();1679 instance.stub.returnsThis();1680 instance.stub.resetBehavior();1681 refute.defined(instance.stub());1682 });1683 describe("does not touch properties that are reset by 'reset'", function () {1684 it(".calledOnce", function () {1685 var stub = createStub();1686 stub(1);1687 stub.resetBehavior();1688 assert(stub.calledOnce);1689 });1690 it("called multiple times", function () {1691 var stub = createStub();1692 stub(1);1693 stub(2);1694 stub(3);1695 stub.resetBehavior();1696 assert(stub.called);1697 assert.equals(stub.args.length, 3);1698 assert.equals(stub.returnValues.length, 3);1699 assert.equals(stub.exceptions.length, 3);1700 assert.equals(stub.thisValues.length, 3);1701 assert.defined(stub.firstCall);1702 assert.defined(stub.secondCall);1703 assert.defined(stub.thirdCall);1704 assert.defined(stub.lastCall);1705 });1706 it("call order state", function () {1707 var stubs = [createStub(), createStub()];1708 stubs[0]();1709 stubs[1]();1710 stubs[0].resetBehavior();1711 assert(stubs[0].calledBefore(stubs[1]));1712 });1713 it("fakes returned by withArgs", function () {1714 var stub = createStub();1715 var fakeA = stub.withArgs("a");1716 var fakeB = stub.withArgs("b");1717 stub("a");1718 stub("b");1719 stub("c");1720 var fakeC = stub.withArgs("c");1721 stub.resetBehavior();1722 assert(fakeA.calledOnce);1723 assert(fakeB.calledOnce);1724 assert(fakeC.calledOnce);1725 });1726 });1727 });1728 describe(".length", function () {1729 it("is zero by default", function () {1730 var stub = createStub();1731 assert.equals(stub.length, 0);1732 });1733 it("matches the function length", function () {1734 var api = { someMethod: function (a, b, c) {} }; // eslint-disable-line no-unused-vars1735 var stub = createStub(api, "someMethod");1736 assert.equals(stub.length, 3);1737 });1738 });1739 describe(".createStubInstance", function () {1740 it("stubs existing methods", function () {1741 var Class = function () {};1742 Class.prototype.method = function () {};1743 var stub = createStubInstance(Class);1744 stub.method.returns(3);1745 assert.equals(3, stub.method());1746 });1747 it("doesn't stub fake methods", function () {1748 var Class = function () {};1749 var stub = createStubInstance(Class);1750 assert.exception(function () {1751 stub.method.returns(3);1752 });1753 });1754 it("doesn't call the constructor", function () {1755 var Class = function (a, b) {1756 var c = a + b;1757 throw c;1758 };1759 Class.prototype.method = function () {};1760 var stub = createStubInstance(Class);1761 refute.exception(function () {1762 stub.method(3);1763 });1764 });1765 it("retains non function values", function () {1766 var TYPE = "some-value";1767 var Class = function () {};1768 Class.prototype.type = TYPE;1769 var stub = createStubInstance(Class);1770 assert.equals(TYPE, stub.type);1771 });1772 it("has no side effects on the prototype", function () {1773 var proto = {1774 method: function () {1775 throw "error";1776 }1777 };1778 var Class = function () {};1779 Class.prototype = proto;1780 var stub = createStubInstance(Class);1781 refute.exception(stub.method);1782 assert.exception(proto.method);1783 });1784 it("throws exception for non function params", function () {1785 var types = [{}, 3, "hi!"];1786 for (var i = 0; i < types.length; i++) {1787 // yes, it's silly to create functions in a loop, it's also a test1788 assert.exception(function () { // eslint-disable-line no-loop-func1789 createStubInstance(types[i]);1790 });1791 }1792 });1793 });1794 describe(".callThrough", function () {1795 it("does not call original function when arguments match conditional stub", function () {1796 // We need a function here because we can't wrap properties that are already stubs1797 var callCount = 0;1798 var originalFunc = function increaseCallCount() {1799 callCount++;1800 };1801 var myObj = {1802 prop: originalFunc1803 };1804 var propStub = createStub(myObj, "prop");1805 propStub.withArgs("foo").returns("bar");1806 propStub.callThrough();1807 var result = myObj.prop("foo");1808 assert.equals(result, "bar");1809 assert.equals(callCount, 0);1810 });1811 it("calls original function when arguments do not match conditional stub", function () {1812 // We need a function here because we can't wrap properties that are already stubs1813 var callCount = 0;1814 var originalFunc = function increaseCallCount() {1815 callCount++;1816 return 1337;1817 };1818 var myObj = {1819 prop: originalFunc1820 };1821 var propStub = createStub(myObj, "prop");1822 propStub.withArgs("foo").returns("bar");1823 propStub.callThrough(propStub);1824 var result = myObj.prop("not foo");1825 assert.equals(result, 1337);1826 assert.equals(callCount, 1);1827 });1828 it("calls original function with same arguments when call does not match conditional stub", function () {1829 // We need a function here because we can't wrap properties that are already stubs1830 var callArgs = [];1831 var originalFunc = function increaseCallCount() {1832 callArgs = arguments;1833 };1834 var myObj = {1835 prop: originalFunc1836 };1837 var propStub = createStub(myObj, "prop");1838 propStub.withArgs("foo").returns("bar");1839 propStub.callThrough();1840 myObj.prop("not foo");1841 assert.equals(callArgs.length, 1);1842 assert.equals(callArgs[0], "not foo");1843 });1844 it("calls original function with same `this` reference when call does not match conditional stub", function () {1845 // We need a function here because we can't wrap properties that are already stubs1846 var reference = {};1847 var originalFunc = function increaseCallCount() {1848 reference = this;1849 };1850 var myObj = {1851 prop: originalFunc1852 };1853 var propStub = createStub(myObj, "prop");1854 propStub.withArgs("foo").returns("bar");1855 propStub.callThrough();1856 myObj.prop("not foo");1857 assert.equals(reference, myObj);1858 });1859 });1860 describe(".get", function () {1861 it("allows users to stub getter functions for properties", function () {1862 var myObj = {1863 prop: "foo"1864 };1865 createStub(myObj, "prop").get(function getterFn() {1866 return "bar";1867 });1868 assert.equals(myObj.prop, "bar");1869 });1870 it("allows users to stub getter functions for functions", function () {1871 var myObj = {1872 prop: function propGetter() {1873 return "foo";1874 }1875 };1876 createStub(myObj, "prop").get(function getterFn() {1877 return "bar";1878 });1879 assert.equals(myObj.prop, "bar");1880 });1881 it("replaces old getters", function () {1882 var myObj = {1883 get prop() {1884 fail("should not call the old getter");1885 }1886 };1887 createStub(myObj, "prop").get(function getterFn() {1888 return "bar";1889 });1890 assert.equals(myObj.prop, "bar");1891 });1892 it("can set getters for non-existing properties", function () {1893 var myObj = {};1894 createStub(myObj, "prop").get(function getterFn() {1895 return "bar";1896 });1897 assert.equals(myObj.prop, "bar");1898 });1899 it("can restore stubbed setters for functions", function () {1900 var propFn = function propFn() {1901 return "bar";1902 };1903 var myObj = {1904 prop: propFn1905 };1906 var stub = createStub(myObj, "prop");1907 stub.get(function getterFn() {1908 return "baz";1909 });1910 stub.restore();1911 assert.equals(myObj.prop, propFn);1912 });1913 it("can restore stubbed getters for properties", function () {1914 var myObj = {1915 get prop() {1916 return "bar";1917 }1918 };1919 var stub = createStub(myObj, "prop");1920 stub.get(function getterFn() {1921 return "baz";1922 });1923 stub.restore();1924 assert.equals(myObj.prop, "bar");1925 });1926 it("can restore stubbed getters for previously undefined properties", function () {1927 var myObj = {};1928 var stub = createStub(myObj, "nonExisting");1929 stub.get(function getterFn() {1930 return "baz";1931 });1932 stub.restore();1933 assert.equals(getPropertyDescriptor(myObj, "nonExisting"), undefined);1934 });1935 });1936 describe(".set", function () {1937 it("allows users to stub setter functions for properties", function () {1938 var myObj = {1939 prop: "foo"1940 };1941 createStub(myObj, "prop").set(function setterFn() {1942 myObj.example = "bar";1943 });1944 myObj.prop = "baz";1945 assert.equals(myObj.example, "bar");1946 });1947 it("allows users to stub setter functions for functions", function () {1948 var myObj = {1949 prop: function propSetter() {1950 return "foo";1951 }1952 };1953 createStub(myObj, "prop").set(function setterFn() {1954 myObj.example = "bar";1955 });1956 myObj.prop = "baz";1957 assert.equals(myObj.example, "bar");1958 });1959 it("replaces old setters", function () {1960 var myObj = { // eslint-disable-line accessor-pairs1961 set prop(val) {1962 fail("should not call the old setter");1963 }1964 };1965 createStub(myObj, "prop").set(function setterFn() {1966 myObj.example = "bar";1967 });1968 myObj.prop = "foo";1969 assert.equals(myObj.example, "bar");1970 });1971 it("can set setters for non-existing properties", function () {1972 var myObj = {};1973 createStub(myObj, "prop").set(function setterFn() {1974 myObj.example = "bar";1975 });1976 myObj.prop = "foo";1977 assert.equals(myObj.example, "bar");1978 });1979 it("can restore stubbed setters for functions", function () {1980 var propFn = function propFn() {1981 return "bar";1982 };1983 var myObj = {1984 prop: propFn1985 };1986 var stub = createStub(myObj, "prop");1987 stub.set(function setterFn() {1988 myObj.otherProp = "baz";1989 });1990 stub.restore();1991 assert.equals(myObj.prop, propFn);1992 });1993 it("can restore stubbed setters for properties", function () {1994 var myObj = { // eslint-disable-line accessor-pairs1995 set prop(val) {1996 this.otherProp = "bar";1997 return "bar";1998 }1999 };2000 var stub = createStub(myObj, "prop");2001 stub.set(function setterFn() {2002 myObj.otherProp = "baz";2003 });2004 stub.restore();2005 myObj.prop = "foo";2006 assert.equals(myObj.otherProp, "bar");2007 });2008 it("can restore stubbed setters for previously undefined properties", function () {2009 var myObj = {};2010 var stub = createStub(myObj, "nonExisting");2011 stub.set(function setterFn() {2012 myObj.otherProp = "baz";2013 });2014 stub.restore();2015 assert.equals(getPropertyDescriptor(myObj, "nonExisting"), undefined);2016 });2017 });2018 describe(".value", function () {2019 it("allows stubbing property descriptor values", function () {2020 var myObj = {2021 prop: "rawString"2022 };2023 createStub(myObj, "prop").value("newString");2024 assert.equals(myObj.prop, "newString");2025 });2026 it("allows restoring stubbed property descriptor values", function () {2027 var myObj = {2028 prop: "rawString"2029 };2030 var stub = createStub(myObj, "prop").value("newString");2031 stub.restore();2032 assert.equals(myObj.prop, "rawString");2033 });2034 it("allows restoring previously undefined properties", function () {2035 var obj = {};2036 var stub = createStub(obj, "nonExisting").value(2);2037 stub.restore();2038 assert.equals(getPropertyDescriptor(obj, "nonExisting"), undefined);2039 });2040 it("allows stubbing function static properties", function () {2041 var myFunc = function () {};2042 myFunc.prop = "rawString";2043 createStub(myFunc, "prop").value("newString");2044 assert.equals(myFunc.prop, "newString");2045 });2046 it("allows restoring function static properties", function () {2047 var myFunc = function () {};2048 myFunc.prop = "rawString";2049 var stub = createStub(myFunc, "prop").value("newString");2050 stub.restore();2051 assert.equals(myFunc.prop, "rawString");2052 });2053 it("allows stubbing object props with configurable false", function () {2054 var myObj = {};2055 Object.defineProperty(myObj, "prop", {2056 configurable: false,2057 enumerable: true,2058 writable: true,2059 value: "static"2060 });2061 createStub(myObj, "prop").value("newString");2062 assert.equals(myObj.prop, "newString");2063 });2064 });...

Full Screen

Full Screen

resources-handle-pull-request.js

Source:resources-handle-pull-request.js Github

copy

Full Screen

1'use strict';2const _ = require('lodash');3const expect = require('chai').expect;4const injectr = require('injectr');5const sinon = require('sinon');6const testData = require('./testData');7describe('resources.handlePullRequest()', () => {8 const emitter = testData.emitterMock;9 const config = testData.configMock;10 const mockedHandlePr = (createStub, updateStub) =>11 injectr('../../src/resources/handle-pull-request.js', {12 '../services/github': () => ({13 createPullRequest: createStub,14 updatePullRequest: updateStub15 })16 })({ emitter, config });17 const repository = testData.postPullRequestFetchInfoRepository;18 describe('when pr not found', () => {19 let err, createStub, updateStub;20 beforeEach(done => {21 createStub = sinon.stub().yields(null, 'ok');22 updateStub = sinon.stub().yields(null, 'ok');23 mockedHandlePr(createStub, updateStub)(_.cloneDeep(repository), error => {24 err = error;25 done();26 });27 });28 it('should not error', () => {29 expect(err).to.be.null;30 });31 it('should create pr', () => {32 expect(createStub.called).to.be.true;33 expect(updateStub.called).to.be.false;34 });35 });36 describe('when pr not found and workingBranch=develop', () => {37 let err, createStub, updateStub;38 beforeEach(done => {39 createStub = sinon.stub().yields(null, 'ok');40 updateStub = sinon.stub().yields(null, 'ok');41 const repo = _.cloneDeep(repository);42 repo.manifestContent.workingBranch = 'develop';43 mockedHandlePr(createStub, updateStub)(repo, error => {44 err = error;45 done();46 });47 });48 it('should not error', () => {49 expect(err).to.be.null;50 });51 it('should create pr to develop branch', () => {52 expect(createStub.args[0][0].base).to.equal('develop');53 expect(createStub.called).to.be.true;54 expect(updateStub.called).to.be.false;55 });56 });57 describe('when pr found but outdated and closed', () => {58 let err, createStub, updateStub;59 beforeEach(done => {60 const repo = _.cloneDeep(repository);61 repo.prInfo = {62 found: true,63 number: 13,64 createdAt: '2017-02-15T15:29:05Z',65 outdated: true,66 closed: true67 };68 createStub = sinon.stub().yields(null, 'ok');69 updateStub = sinon.stub().yields(null, 'ok');70 mockedHandlePr(createStub, updateStub)(repo, error => {71 err = error;72 done();73 });74 });75 it('should not error', () => {76 expect(err).to.be.null;77 });78 it('should create pr', () => {79 expect(createStub.called).to.be.true;80 expect(updateStub.called).to.be.false;81 });82 });83 describe('when pr found and valid', () => {84 let err, createStub, updateStub;85 beforeEach(done => {86 const repo = _.cloneDeep(repository);87 repo.prInfo = {88 found: true,89 number: 13,90 createdAt: '2017-02-15T15:29:05Z',91 outdated: false92 };93 createStub = sinon.stub().yields(null, 'ok');94 updateStub = sinon.stub().yields(null, 'ok');95 mockedHandlePr(createStub, updateStub)(repo, error => {96 err = error;97 done();98 });99 });100 it('should not error', () => {101 expect(err).to.be.null;102 });103 it('should update pr', () => {104 expect(createStub.called).to.be.false;105 expect(updateStub.called).to.be.true;106 expect(updateStub.args[0][0].pull_number).to.equal(13);107 });108 });109 describe('when pr found and valid and workingBranch=develop', () => {110 let err, createStub, updateStub;111 beforeEach(done => {112 const repo = _.cloneDeep(repository);113 repo.prInfo = {114 found: true,115 number: 13,116 createdAt: '2017-02-15T15:29:05Z',117 outdated: false118 };119 repo.manifestContent.workingBranch = 'develop';120 createStub = sinon.stub().yields(null, 'ok');121 updateStub = sinon.stub().yields(null, 'ok');122 mockedHandlePr(createStub, updateStub)(repo, error => {123 err = error;124 done();125 });126 });127 it('should not error', () => {128 expect(err).to.be.null;129 });130 it('should update pr to develop branch', () => {131 expect(createStub.called).to.be.false;132 expect(updateStub.called).to.be.true;133 expect(updateStub.args[0][0].base).to.equal('develop');134 expect(updateStub.args[0][0].pull_number).to.equal(13);135 });136 });137 describe('when pr create fails', () => {138 let err, createStub, updateStub;139 beforeEach(done => {140 const repo = _.cloneDeep(repository);141 repo.prInfo = {142 found: true,143 number: 13,144 createdAt: '2017-02-15T15:29:05Z',145 outdated: true,146 closed: true147 };148 createStub = sinon.stub().yields('an error');149 updateStub = sinon.stub().yields(null, 'ok');150 mockedHandlePr(createStub, updateStub)(repo, error => {151 err = error;152 done();153 });154 });155 it('should show an error', () => {156 expect(err.toString()).to.contain('Failed while creating pull request');157 });158 });159 describe('when pr update fails', () => {160 let err, createStub, updateStub;161 beforeEach(done => {162 const repo = _.cloneDeep(repository);163 repo.prInfo = {164 found: true,165 number: 13,166 createdAt: '2017-02-15T15:29:05Z',167 outdated: false168 };169 createStub = sinon.stub().yields(null, 'ok');170 updateStub = sinon.stub().yields('an error');171 mockedHandlePr(createStub, updateStub)(repo, error => {172 err = error;173 done();174 });175 });176 it('should show an error', () => {177 expect(err.toString()).to.contain('Failed while updating pull request');178 });179 });180 describe('when the skipPullRequest flag is true', () => {181 let err, createStub, updateStub;182 beforeEach(done => {183 const repo = _.cloneDeep(repository);184 repo.skipPullRequest = true;185 createStub = sinon.stub().yields(null, 'ok');186 updateStub = sinon.stub().yields(null, 'ok');187 mockedHandlePr(createStub, updateStub)(repo, error => {188 err = error;189 done();190 });191 });192 it('should not error', () => {193 expect(err).to.be.null;194 });195 it('should not create or update any pr', () => {196 expect(createStub.called).to.be.false;197 expect(updateStub.called).to.be.false;198 });199 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var stub = sinon.createStub();3stub();4console.log(stub.called);5var sinon = require('sinon');6var stub = sinon.createStubInstance(Date);7stub.toUTCString();8console.log(stub.toUTCString.called);9var sinon = require('sinon');10var fake = sinon.fake();11fake();12console.log(fake.called);13var sinon = require('sinon');14var server = sinon.fakeServer.create();15server.respondWith(16 [200, { "Content-Type": "application/json" }, '{ "foo": "bar" }']17);18server.respond();19console.log(server.requests[0].url);20console.log(server.requests[0].method);21var sinon = require('sinon');22var clock = sinon.useFakeTimers();23var server = sinon.fakeServer.create();24server.respondWith(25 [200, { "Content-Type": "application/json" }, '{ "foo": "bar" }']26);27server.respond();28console.log(server.requests[0].url);29console.log(server.requests[0].method);30var sinon = require('sinon');31var clock = sinon.useFakeTimers();32console.log(Date.now());33clock.tick(100);34console.log(Date.now());35var sinon = require('sinon');36var fake = sinon.fake.yieldsTo("callback", "arg1", "arg2");37fake(function callback(arg1, arg2){38});39var sinon = require('sinon');40var fake = sinon.fake.yields("arg1", "arg2");41fake(function callback(arg1, arg2){42});

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var foo = { 3 setName: function(name) { 4 this.name = name;5 }, 6 setAge: function(age) { 7 this.age = age;8 }9};10var stub = sinon.createStubInstance(foo);11stub.setName('foo');12stub.setAge(25);13var sinon = require('sinon');14var foo = function(name, age) { 15 this.name = name;16 this.age = age;17};18var stub = sinon.createStubInstance(foo);19stub.setName('foo');20stub.setAge(25);21var sinon = require('sinon');22var foo = { 23 setName: function(name) { 24 this.name = name;25 }, 26 setAge: function(age) { 27 this.age = age;28 }29};30var stub = sinon.createStubInstance(foo);31stub.setName('foo');32stub.setAge(25);33console.log(stub

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var stub = sinon.stub();3stub('foo');4var sinon = require('sinon');5var stub = sinon.createStubInstance(Bar);6stub.foo('foo');7var sinon = require('sinon');8var stub = sinon.createStubInstance(Bar);9stub.foo('foo');10var sinon = require('sinon');11var stub = sinon.createStubInstance(Bar);12stub.foo('foo');13var sinon = require('sinon');14var stub = sinon.createStubInstance(Bar);15stub.foo('foo');16var sinon = require('sinon');17var stub = sinon.createStubInstance(Bar);18stub.foo('foo');19var sinon = require('sinon');20var stub = sinon.createStubInstance(Bar);21stub.foo('foo');22var sinon = require('sinon');23var stub = sinon.createStubInstance(Bar);24stub.foo('foo');25var sinon = require('sinon');26var stub = sinon.createStubInstance(Bar);27stub.foo('foo');28var sinon = require('sinon');29var stub = sinon.createStubInstance(Bar);30stub.foo('foo');31var sinon = require('sinon');32var stub = sinon.createStubInstance(Bar);33stub.foo('foo');34var sinon = require('sinon');35var stub = sinon.createStubInstance(Bar);36stub.foo('

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var obj = {3 method: function() {4 console.log('real method');5 }6};7var stub = sinon.stub(obj, 'method');8stub.returns(42);9var sinon = require('sinon');10var obj = sinon.createStubInstance(MyConstructor);11obj.myMethod.returns(42);12var sinon = require('sinon');13var obj = {14 method: function() {15 console.log('real method');16 }17};18var stub = sinon.stub(obj, 'method', function() {19 console.log('stub method');20});21var sinon = require('sinon');22var obj = {23 method: function() {24 console.log('real method');25 }26};27var stub = sinon.stub(obj, 'method', function() {28 console.log('stub method');29});30var sinon = require('sinon');31var obj = {32 method: function() {33 console.log('real method');34 }35};36var stub = sinon.stub(obj, 'method', function() {37 console.log('stub method');38});39var sinon = require('sinon');40var obj = {41 method: function() {42 console.log('real method');43 }44};45var stub = sinon.stub(obj, 'method', function() {46 console.log('stub method');47});48var sinon = require('sinon');49var obj = {50 method: function() {51 console.log('real method');52 }53};54var stub = sinon.stub(obj, 'method', function() {55 console.log('stub method');56});57var sinon = require('sinon');58var obj = {59 method: function() {60 console.log('real method');61 }62};63var stub = sinon.stub(obj, 'method', function() {64 console.log('stub

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var assert = require('assert');3var fs = require('fs');4var stub = sinon.stub(fs, 'readFile');5stub.yields(null, 'hello world');6fs.readFile('foo.txt', function(err, data) {7 assert.equal(data, 'hello world');8});9stub.restore();10fs.readFile('foo.txt', function(err, data) {11 assert.equal(data, 'foo');12});

Full Screen

Using AI Code Generation

copy

Full Screen

1var stub = sinon.createStubInstance(object);2var stub = sinon.createStubInstance(object);3var stub = sinon.createStubInstance(object);4var stub = sinon.createStubInstance(object);5var stub = sinon.createStubInstance(object);6var stub = sinon.createStubInstance(object);7var stub = sinon.createStubInstance(object);8var stub = sinon.createStubInstance(object);9var stub = sinon.createStubInstance(object);10var stub = sinon.createStubInstance(object);11var stub = sinon.createStubInstance(object);12var stub = sinon.createStubInstance(object);13var stub = sinon.createStubInstance(object);14var stub = sinon.createStubInstance(object);15var stub = sinon.createStubInstance(object);16var stub = sinon.createStubInstance(object);17var stub = sinon.createStubInstance(object);18var stub = sinon.createStubInstance(object);19var stub = sinon.createStubInstance(object);20var stub = sinon.createStubInstance(object);21var stub = sinon.createStubInstance(object);

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var myObj = {3 myMethod: function() {4 console.log("myMethod called");5 }6};7var stub = sinon.stub(myObj, "myMethod");8myObj.myMethod();9stub.restore();10myObj.myMethod();11var sinon = require('sinon');12var myObj = function() {13 this.myMethod = function() {14 console.log("myMethod called");15 };16};17var stub = sinon.createStubInstance(myObj);18stub.myMethod();19stub.restore();20stub.myMethod();21var sinon = require('sinon');22var myObj = {23 myMethod: function() {24 console.log("myMethod called");25 }26};27var stub = sinon.stub(myObj);28stub.myMethod();29stub.restore();30stub.myMethod();

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var assert = require('assert');3var myModule = require('./myModule');4describe('myModule', function() {5 it('should return value from stub', function() {6 var stub = sinon.stub(myModule, 'myMethod');7 stub.onCall(0).returns(1);8 stub.onCall(1).returns(2);9 stub.onCall(2).returns(3);10 assert.equal(myModule.myMethod(), 1);11 assert.equal(myModule.myMethod(), 2);12 assert.equal(myModule.myMethod(), 3);13 });14});15exports.myMethod = function() {16 return 1;17}18 1 passing (8ms)19We have tested the myMethod() function of myModule.js. We have also used the onCall() method to return different values on different calls. We can also use the onCall() method to throw an error on a specific call. Here is an example:20var sinon = require('sinon');21var assert = require('assert');22var myModule = require('./myModule');23describe('myModule', function() {24 it('should return value from stub', function() {25 var stub = sinon.stub(myModule, 'myMethod');26 stub.onCall(0).returns(1);27 stub.onCall(1).returns(2);28 stub.onCall(2).throws(new Error('Error thrown by myMethod'));29 assert.equal(myModule.myMethod(), 1);30 assert.equal(myModule.myMethod(), 2);31 assert.throws(function() {32 myModule.myMethod();33 }, Error, 'Error thrown by myMethod');34 });35});36exports.myMethod = function() {

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