Best JavaScript code snippet using sinon
sandbox-test.js
Source:sandbox-test.js
...66 assert.equals(typeof actual.verify, "function");67 assert.equals(typeof object.method.restore, "function");68 });69 it("adds mock to fake array", function() {70 var fakes = this.sandbox.getFakes();71 var object = {72 method: function() {73 return;74 }75 };76 var expected = this.sandbox.mock(object);77 assert(fakes.indexOf(expected) !== -1);78 });79 it("appends mocks to fake array", function() {80 var fakes = this.sandbox.getFakes();81 this.sandbox.mock({});82 this.sandbox.mock({});83 assert.equals(fakes.length, 2);84 });85 });86 describe("stub and mock test", function() {87 beforeEach(function() {88 this.sandbox = createSandbox();89 });90 it("appends mocks and stubs to fake array", function() {91 var fakes = this.sandbox.getFakes();92 this.sandbox.mock(93 {94 method: function() {95 return;96 }97 },98 "method"99 );100 this.sandbox.stub(101 {102 method: function() {103 return;104 }105 },106 "method"107 );108 assert.equals(fakes.length, 2);109 });110 });111 describe(".spy", function() {112 it("should return a spy", function() {113 var sandbox = createSandbox();114 var spy = sandbox.spy();115 assert.isFunction(spy);116 assert.equals(spy.displayName, "spy");117 });118 it("should add a spy to the internal collection", function() {119 var sandbox = createSandbox();120 var fakes = sandbox.getFakes();121 var expected;122 expected = sandbox.spy();123 assert.isTrue(fakes.indexOf(expected) !== -1);124 });125 });126 describe(".createStubInstance", function() {127 beforeEach(function() {128 this.sandbox = createSandbox();129 });130 it("stubs existing methods", function() {131 var Class = function() {132 return;133 };134 Class.prototype.method = function() {135 return;136 };137 var stub = this.sandbox.createStubInstance(Class);138 stub.method.returns(3);139 assert.equals(3, stub.method());140 });141 it("should require a function", function() {142 var sandbox = this.sandbox;143 assert.exception(144 function() {145 sandbox.createStubInstance("not a function");146 },147 {148 name: "TypeError",149 message: "The constructor should be a function."150 }151 );152 });153 it("resets all stub methods on reset()", function() {154 var Class = function() {155 return;156 };157 Class.prototype.method1 = function() {158 return;159 };160 Class.prototype.method2 = function() {161 return;162 };163 Class.prototype.method3 = function() {164 return;165 };166 var stub = this.sandbox.createStubInstance(Class);167 stub.method1.returns(1);168 stub.method2.returns(2);169 stub.method3.returns(3);170 assert.equals(3, stub.method3());171 this.sandbox.reset();172 assert.isUndefined(stub.method1());173 assert.isUndefined(stub.method2());174 assert.isUndefined(stub.method3());175 });176 it("doesn't stub fake methods", function() {177 var Class = function() {178 return;179 };180 Class.prototype.noop = noop;181 var stub = this.sandbox.createStubInstance(Class);182 assert.exception(function() {183 stub.method.returns(3);184 });185 });186 it("doesn't call the constructor", function() {187 var Class = function(a, b) {188 var c = a + b;189 throw c;190 };191 Class.prototype.method = function() {192 return;193 };194 var stub = this.sandbox.createStubInstance(Class);195 refute.exception(function() {196 stub.method(3);197 });198 });199 it("retains non function values", function() {200 var TYPE = "some-value";201 var Class = function() {202 return;203 };204 Class.prototype.noop = noop;205 Class.prototype.type = TYPE;206 var stub = this.sandbox.createStubInstance(Class);207 assert.equals(TYPE, stub.type);208 });209 it("has no side effects on the prototype", function() {210 var proto = {211 method: function() {212 throw new Error("error");213 }214 };215 var Class = function() {216 return;217 };218 Class.prototype = proto;219 var stub = this.sandbox.createStubInstance(Class);220 refute.exception(stub.method);221 assert.exception(proto.method);222 });223 it("throws exception for non function params", function() {224 var types = [{}, 3, "hi!"];225 for (var i = 0; i < types.length; i++) {226 // yes, it's silly to create functions in a loop, it's also a test227 /* eslint-disable-next-line ie11/no-loop-func, no-loop-func */228 assert.exception(function() {229 this.sandbox.createStubInstance(types[i]);230 });231 }232 });233 it("allows providing optional overrides", function() {234 var Class = function() {235 return;236 };237 Class.prototype.method = function() {238 return;239 };240 var stub = this.sandbox.createStubInstance(Class, {241 method: sinonStub().returns(3)242 });243 assert.equals(3, stub.method());244 });245 it("allows providing optional returned values", function() {246 var Class = function() {247 return;248 };249 Class.prototype.method = function() {250 return;251 };252 var stub = this.sandbox.createStubInstance(Class, {253 method: 3254 });255 assert.equals(3, stub.method());256 });257 it("allows providing null as a return value", function() {258 var Class = function() {259 return;260 };261 Class.prototype.method = function() {262 return;263 };264 var stub = this.sandbox.createStubInstance(Class, {265 method: null266 });267 assert.equals(null, stub.method());268 });269 it("throws an exception when trying to override non-existing property", function() {270 var Class = function() {271 return;272 };273 Class.prototype.method = function() {274 return;275 };276 var sandbox = this.sandbox;277 assert.exception(278 function() {279 sandbox.createStubInstance(Class, {280 foo: sinonStub().returns(3)281 });282 },283 { message: "Cannot stub foo. Property does not exist!" }284 );285 });286 });287 describe(".stub", function() {288 beforeEach(function() {289 this.sandbox = createSandbox();290 });291 it("fails if stubbing property on null", function() {292 var sandbox = this.sandbox;293 assert.exception(294 function() {295 sandbox.stub(null, "prop");296 },297 {298 message: "Trying to stub property 'prop' of null"299 }300 );301 });302 it("fails if stubbing symbol on null", function() {303 if (typeof Symbol === "function") {304 var sandbox = this.sandbox;305 assert.exception(306 function() {307 sandbox.stub(null, Symbol());308 },309 {310 message: "Trying to stub property 'Symbol()' of null"311 }312 );313 }314 });315 it("creates a stub", function() {316 var object = {317 method: function() {318 return;319 }320 };321 this.sandbox.stub(object, "method");322 assert.equals(typeof object.method.restore, "function");323 });324 it("adds stub to fake array", function() {325 var fakes = this.sandbox.getFakes();326 var object = {327 method: function() {328 return;329 }330 };331 var stub = this.sandbox.stub(object, "method");332 assert.isTrue(fakes.indexOf(stub) !== -1);333 });334 it("appends stubs to fake array", function() {335 var fakes = this.sandbox.getFakes();336 this.sandbox.stub(337 {338 method: function() {339 return;340 }341 },342 "method"343 );344 this.sandbox.stub(345 {346 method: function() {347 return;348 }349 },350 "method"351 );352 assert.equals(fakes.length, 2);353 });354 it("adds all object methods to fake array", function() {355 var fakes = this.sandbox.getFakes();356 var object = {357 method: function() {358 return;359 },360 method2: function() {361 return;362 },363 method3: function() {364 return;365 }366 };367 Object.defineProperty(object, "method3", {368 enumerable: false369 });370 this.sandbox.stub(object);371 assert.isTrue(fakes.indexOf(object.method) !== -1);372 assert.isTrue(fakes.indexOf(object.method2) !== -2);373 assert.isTrue(fakes.indexOf(object.method3) !== -3);374 assert.equals(fakes.length, 3);375 });376 it("returns a stubbed object", function() {377 var object = {378 method: function() {379 return;380 }381 };382 assert.equals(this.sandbox.stub(object), object);383 });384 it("returns a stubbed method", function() {385 var object = {386 method: function() {387 return;388 }389 };390 assert.equals(this.sandbox.stub(object, "method"), object.method);391 });392 describe("on node", function() {393 before(function() {394 if (typeof process === "undefined" || !process.env) {395 this.skip();396 }397 });398 beforeEach(function() {399 process.env.HELL = "Ain't too bad";400 });401 it("stubs environment property", function() {402 var originalPrintWarning = deprecated.printWarning;403 deprecated.printWarning = function() {404 return;405 };406 this.sandbox.stub(process.env, "HELL").value("froze over");407 assert.equals(process.env.HELL, "froze over");408 deprecated.printWarning = originalPrintWarning;409 });410 });411 });412 describe("stub anything", function() {413 beforeEach(function() {414 this.object = { property: 42 };415 this.sandbox = new Sandbox();416 });417 it("stubs number property", function() {418 var originalPrintWarning = deprecated.printWarning;419 deprecated.printWarning = function() {420 return;421 };422 this.sandbox.stub(this.object, "property").value(1);423 assert.equals(this.object.property, 1);424 deprecated.printWarning = originalPrintWarning;425 });426 it("restores number property", function() {427 var originalPrintWarning = deprecated.printWarning;428 deprecated.printWarning = function() {429 return;430 };431 this.sandbox.stub(this.object, "property").value(1);432 this.sandbox.restore();433 assert.equals(this.object.property, 42);434 deprecated.printWarning = originalPrintWarning;435 });436 it("fails if property does not exist", function() {437 var originalPrintWarning = deprecated.printWarning;438 deprecated.printWarning = function() {439 return;440 };441 var sandbox = this.sandbox;442 var object = {};443 assert.exception(function() {444 sandbox.stub(object, "prop", 1);445 });446 deprecated.printWarning = originalPrintWarning;447 });448 it("fails if Symbol does not exist", function() {449 if (typeof Symbol === "function") {450 var sandbox = this.sandbox;451 var object = {};452 var originalPrintWarning = deprecated.printWarning;453 deprecated.printWarning = function() {454 return;455 };456 assert.exception(457 function() {458 sandbox.stub(object, Symbol());459 },460 { message: "Cannot stub non-existent property Symbol()" },461 TypeError462 );463 deprecated.printWarning = originalPrintWarning;464 }465 });466 });467 describe(".fake", function() {468 it("should return a fake", function() {469 var sandbox = createSandbox();470 var fake = sandbox.fake();471 assert.isFunction(fake);472 assert.equals(fake.displayName, "fake");473 });474 it("should add a fake to the internal collection", function() {475 var sandbox = createSandbox();476 var fakes = sandbox.getFakes();477 var expected;478 expected = sandbox.fake();479 assert.isTrue(fakes.indexOf(expected) !== -1);480 });481 describe(".returns", function() {482 it("should return a fake behavior", function() {483 var sandbox = createSandbox();484 var fake = sandbox.fake.returns();485 assert.isFunction(fake);486 assert.equals(fake.displayName, "fake");487 });488 it("should add a fake behavior to the internal collection", function() {489 var sandbox = createSandbox();490 var fakes = sandbox.getFakes();491 var expected;492 expected = sandbox.fake.returns();493 assert.isTrue(fakes.indexOf(expected) !== -1);494 });495 });496 describe(".throws", function() {497 it("should return a fake behavior", function() {498 var sandbox = createSandbox();499 var fake = sandbox.fake.throws();500 assert.isFunction(fake);501 assert.equals(fake.displayName, "fake");502 });503 it("should add a fake behavior to the internal collection", function() {504 var sandbox = createSandbox();505 var fakes = sandbox.getFakes();506 var expected;507 expected = sandbox.fake.throws();508 assert.isTrue(fakes.indexOf(expected) !== -1);509 });510 });511 describe(".resolves", function() {512 it("should return a fake behavior", function() {513 var sandbox = createSandbox();514 var fake = sandbox.fake.resolves();515 assert.isFunction(fake);516 assert.equals(fake.displayName, "fake");517 });518 it("should add a fake behavior to the internal collection", function() {519 var sandbox = createSandbox();520 var fakes = sandbox.getFakes();521 var expected;522 expected = sandbox.fake.resolves();523 assert.isTrue(fakes.indexOf(expected) !== -1);524 });525 });526 describe(".rejects", function() {527 it("should return a fake behavior", function() {528 var sandbox = createSandbox();529 var fake = sandbox.fake.rejects();530 assert.isFunction(fake);531 assert.equals(fake.displayName, "fake");532 });533 it("should add a fake behavior to the internal collection", function() {534 var sandbox = createSandbox();535 var fakes = sandbox.getFakes();536 var expected;537 expected = sandbox.fake.rejects();538 assert.isTrue(fakes.indexOf(expected) !== -1);539 });540 });541 describe(".yields", function() {542 it("should return a fake behavior", function() {543 var sandbox = createSandbox();544 var fake = sandbox.fake.yields();545 assert.isFunction(fake);546 assert.equals(fake.displayName, "fake");547 });548 it("should add a fake behavior to the internal collection", function() {549 var sandbox = createSandbox();550 var fakes = sandbox.getFakes();551 var expected;552 expected = sandbox.fake.yields();553 assert.isTrue(fakes.indexOf(expected) !== -1);554 });555 });556 describe(".yieldsAsync", function() {557 it("should return a fake behavior", function() {558 var sandbox = createSandbox();559 var fake = sandbox.fake.yieldsAsync();560 assert.isFunction(fake);561 assert.equals(fake.displayName, "fake");562 });563 it("should add a fake behavior to the internal collection", function() {564 var sandbox = createSandbox();565 var fakes = sandbox.getFakes();566 var expected;567 expected = sandbox.fake.yieldsAsync();568 assert.isTrue(fakes.indexOf(expected) !== -1);569 });570 });571 });572 describe(".verifyAndRestore", function() {573 beforeEach(function() {574 this.sandbox = createSandbox();575 });576 it("calls verify and restore", function() {577 this.sandbox.verify = sinonSpy();578 this.sandbox.restore = sinonSpy();579 this.sandbox.verifyAndRestore();580 assert(this.sandbox.verify.called);581 assert(this.sandbox.restore.called);582 });583 it("throws when restore throws", function() {584 this.sandbox.verify = sinonSpy();585 this.sandbox.restore = sinonStub().throws();586 assert.exception(function() {587 this.sandbox.verifyAndRestore();588 });589 });590 it("calls restore when restore throws", function() {591 var sandbox = this.sandbox;592 sandbox.verify = sinonSpy();593 sandbox.restore = sinonStub().throws();594 assert.exception(function() {595 sandbox.verifyAndRestore();596 });597 assert(sandbox.restore.called);598 });599 });600 describe(".replace", function() {601 beforeEach(function() {602 this.sandbox = createSandbox();603 });604 it("should replace a function property", function() {605 var replacement = function replacement() {606 return;607 };608 var existing = function existing() {609 return;610 };611 var object = {612 property: existing613 };614 this.sandbox.replace(object, "property", replacement);615 assert.equals(object.property, replacement);616 this.sandbox.restore();617 assert.equals(object.property, existing);618 });619 it("should replace a non-function property", function() {620 var replacement = "replacement";621 var existing = "existing";622 var object = {623 property: existing624 };625 this.sandbox.replace(object, "property", replacement);626 assert.equals(object.property, replacement);627 this.sandbox.restore();628 assert.equals(object.property, existing);629 });630 it("should replace an inherited property", function() {631 var replacement = "replacement";632 var existing = "existing";633 var object = Object.create({634 property: existing635 });636 this.sandbox.replace(object, "property", replacement);637 assert.equals(object.property, replacement);638 this.sandbox.restore();639 assert.equals(object.property, existing);640 });641 it("should error on missing descriptor", function() {642 var sandbox = this.sandbox;643 assert.exception(644 function() {645 sandbox.replace({}, "i-dont-exist");646 },647 {648 message: "Cannot replace non-existent property i-dont-exist",649 name: "TypeError"650 }651 );652 });653 it("should error on missing replacement", function() {654 var sandbox = this.sandbox;655 var object = Object.create({656 property: "catpants"657 });658 assert.exception(659 function() {660 sandbox.replace(object, "property");661 },662 {663 message: "Expected replacement argument to be defined",664 name: "TypeError"665 }666 );667 });668 it("should refuse to replace a non-function with a function", function() {669 var sandbox = this.sandbox;670 var replacement = function() {671 return "replacement";672 };673 var existing = "existing";674 var object = {675 property: existing676 };677 assert.exception(678 function() {679 sandbox.replace(object, "property", replacement);680 },681 { message: "Cannot replace string with function" }682 );683 });684 it("should refuse to replace a function with a non-function", function() {685 var sandbox = this.sandbox;686 var replacement = "replacement";687 var object = {688 property: function() {689 return "apple pie";690 }691 };692 assert.exception(693 function() {694 sandbox.replace(object, "property", replacement);695 },696 { message: "Cannot replace function with string" }697 );698 });699 it("should refuse to replace a fake twice", function() {700 var sandbox = this.sandbox;701 var object = {702 property: function() {703 return "apple pie";704 }705 };706 sandbox.replace(object, "property", sinonFake());707 assert.exception(708 function() {709 sandbox.replace(object, "property", sinonFake());710 },711 { message: "Attempted to replace property which is already replaced" }712 );713 });714 it("should refuse to replace a string twice", function() {715 var sandbox = this.sandbox;716 var object = {717 property: "original"718 };719 sandbox.replace(object, "property", "first");720 assert.exception(721 function() {722 sandbox.replace(object, "property", "second");723 },724 { message: "Attempted to replace property which is already replaced" }725 );726 });727 it("should return the replacement argument", function() {728 var replacement = "replacement";729 var existing = "existing";730 var object = {731 property: existing732 };733 var actual = this.sandbox.replace(object, "property", replacement);734 assert.equals(actual, replacement);735 });736 describe("when asked to replace a getter", function() {737 it("should throw an Error", function() {738 var sandbox = this.sandbox;739 var object = {740 get foo() {741 return "bar";742 }743 };744 assert.exception(745 function() {746 sandbox.replace(object, "foo", sinonFake());747 },748 { message: "Use sandbox.replaceGetter for replacing getters" }749 );750 });751 });752 describe("when asked to replace a setter", function() {753 it("should throw an Error", function() {754 var sandbox = this.sandbox;755 var object = {756 // eslint-disable-next-line accessor-pairs757 set foo(value) {758 this.prop = value;759 }760 };761 assert.exception(762 function() {763 sandbox.replace(object, "foo", sinonFake());764 },765 { message: "Use sandbox.replaceSetter for replacing setters" }766 );767 });768 });769 });770 describe(".replaceGetter", function() {771 beforeEach(function() {772 this.sandbox = createSandbox();773 });774 it("should replace getters", function() {775 var expected = "baz";776 var object = {777 get foo() {778 return "bar";779 }780 };781 this.sandbox.replaceGetter(object, "foo", sinonFake.returns(expected));782 assert.equals(object.foo, expected);783 });784 it("should return replacement", function() {785 var replacement = sinonFake.returns("baz");786 var object = {787 get foo() {788 return "bar";789 }790 };791 var actual = this.sandbox.replaceGetter(object, "foo", replacement);792 assert.equals(actual, replacement);793 });794 it("should replace an inherited property", function() {795 var expected = "baz";796 var replacement = sinonFake.returns(expected);797 var existing = "existing";798 var object = Object.create({799 get foo() {800 return existing;801 }802 });803 this.sandbox.replaceGetter(object, "foo", replacement);804 assert.equals(object.foo, expected);805 this.sandbox.restore();806 assert.equals(object.foo, existing);807 });808 it("should error on missing descriptor", function() {809 var sandbox = this.sandbox;810 assert.exception(811 function() {812 sandbox.replaceGetter({}, "i-dont-exist");813 },814 {815 message: "Cannot replace non-existent property i-dont-exist",816 name: "TypeError"817 }818 );819 });820 it("should error when descriptor has no getter", function() {821 var sandbox = this.sandbox;822 var object = {823 // eslint-disable-next-line accessor-pairs824 set catpants(_) {825 return;826 }827 };828 assert.exception(829 function() {830 sandbox.replaceGetter(object, "catpants", noop);831 },832 {833 message: "`object.property` is not a getter",834 name: "Error"835 }836 );837 });838 describe("when called with a non-function replacement argument", function() {839 it("should throw a TypeError", function() {840 var sandbox = this.sandbox;841 var expected = "baz";842 var object = {843 get foo() {844 return "bar";845 }846 };847 assert.exception(848 function() {849 sandbox.replaceGetter(object, "foo", expected);850 },851 { message: "Expected replacement argument to be a function" }852 );853 });854 });855 it("allows restoring getters", function() {856 var expected = "baz";857 var object = {858 get foo() {859 return "bar";860 }861 };862 this.sandbox.replaceGetter(object, "foo", sinonFake.returns(expected));863 this.sandbox.restore();864 assert.equals(object.foo, "bar");865 });866 it("should refuse to replace a getter twice", function() {867 var sandbox = this.sandbox;868 var object = {869 get foo() {870 return "bar";871 }872 };873 sandbox.replaceGetter(object, "foo", sinonFake.returns("one"));874 assert.exception(875 function() {876 sandbox.replaceGetter(object, "foo", sinonFake.returns("two"));877 },878 { message: "Attempted to replace foo which is already replaced" }879 );880 });881 });882 describe(".replaceSetter", function() {883 beforeEach(function() {884 this.sandbox = createSandbox();885 });886 it("should replace setter", function() {887 var object = {888 // eslint-disable-next-line accessor-pairs889 set foo(value) {890 this.prop = value;891 },892 prop: "bar"893 };894 this.sandbox.replaceSetter(object, "foo", function(val) {895 this.prop = val + "bla";896 });897 object.foo = "bla";898 assert.equals(object.prop, "blabla");899 });900 it("should return replacement", function() {901 var object = {902 // eslint-disable-next-line accessor-pairs903 set foo(value) {904 this.prop = value;905 },906 prop: "bar"907 };908 var replacement = function(val) {909 this.prop = val + "bla";910 };911 var actual = this.sandbox.replaceSetter(object, "foo", replacement);912 assert.equals(actual, replacement);913 });914 it("should replace an inherited property", function() {915 var object = Object.create({916 // eslint-disable-next-line accessor-pairs917 set foo(value) {918 this.prop = value;919 },920 prop: "bar"921 });922 var replacement = function(value) {923 this.prop = value + "blabla";924 };925 this.sandbox.replaceSetter(object, "foo", replacement);926 object.foo = "doodle";927 assert.equals(object.prop, "doodleblabla");928 this.sandbox.restore();929 object.foo = "doodle";930 assert.equals(object.prop, "doodle");931 });932 it("should error on missing descriptor", function() {933 var sandbox = this.sandbox;934 assert.exception(935 function() {936 sandbox.replaceSetter({}, "i-dont-exist");937 },938 {939 message: "Cannot replace non-existent property i-dont-exist",940 name: "TypeError"941 }942 );943 });944 it("should error when descriptor has no setter", function() {945 var sandbox = this.sandbox;946 var object = {947 get catpants() {948 return;949 }950 };951 assert.exception(952 function() {953 sandbox.replaceSetter(object, "catpants", noop);954 },955 {956 message: "`object.property` is not a setter",957 name: "Error"958 }959 );960 });961 describe("when called with a non-function replacement argument", function() {962 it("should throw a TypeError", function() {963 var sandbox = this.sandbox;964 var object = {965 // eslint-disable-next-line accessor-pairs966 set foo(value) {967 this.prop = value;968 },969 prop: "bar"970 };971 assert.exception(972 function() {973 sandbox.replaceSetter(object, "foo", "bla");974 },975 { message: "Expected replacement argument to be a function" }976 );977 });978 });979 it("allows restoring setters", function() {980 var object = {981 // eslint-disable-next-line accessor-pairs982 set foo(value) {983 this.prop = value;984 },985 prop: "bar"986 };987 this.sandbox.replaceSetter(object, "foo", function(val) {988 this.prop = val + "bla";989 });990 this.sandbox.restore();991 object.prop = "bla";992 assert.equals(object.prop, "bla");993 });994 it("should refuse to replace a setter twice", function() {995 var sandbox = this.sandbox;996 var object = {997 // eslint-disable-next-line accessor-pairs998 set foo(value) {999 return;1000 }1001 };1002 sandbox.replaceSetter(object, "foo", sinonFake());1003 assert.exception(1004 function() {1005 sandbox.replaceSetter(object, "foo", sinonFake.returns("two"));1006 },1007 { message: "Attempted to replace foo which is already replaced" }1008 );1009 });1010 });1011 describe(".reset", function() {1012 beforeEach(function() {1013 var sandbox = (this.sandbox = createSandbox());1014 var fakes = sandbox.getFakes();1015 fakes.push({1016 reset: sinonSpy(),1017 resetHistory: sinonSpy()1018 });1019 fakes.push({1020 reset: sinonSpy(),1021 resetHistory: sinonSpy()1022 });1023 });1024 it("calls reset on all fakes", function() {1025 var fake0 = this.sandbox.getFakes()[0];1026 var fake1 = this.sandbox.getFakes()[1];1027 this.sandbox.reset();1028 assert(fake0.reset.called);1029 assert(fake1.reset.called);1030 });1031 it("calls resetHistory on all fakes", function() {1032 var fake0 = this.sandbox.getFakes()[0];1033 var fake1 = this.sandbox.getFakes()[1];1034 this.sandbox.reset();1035 assert(fake0.resetHistory.called);1036 assert(fake1.resetHistory.called);1037 });1038 it("resets fake behaviours", function() {1039 var fake = this.sandbox.fake();1040 fake(1234);1041 this.sandbox.reset();1042 assert.equals(fake.getCalls(), []);1043 });1044 });1045 describe(".resetBehavior", function() {1046 beforeEach(function() {1047 var sandbox = (this.sandbox = createSandbox());1048 var fakes = sandbox.getFakes();1049 fakes.push({ resetBehavior: sinonSpy() });1050 fakes.push({ resetBehavior: sinonSpy() });1051 });1052 it("calls resetBehavior on all fakes", function() {1053 var fake0 = this.sandbox.getFakes()[0];1054 var fake1 = this.sandbox.getFakes()[1];1055 this.sandbox.resetBehavior();1056 assert(fake0.resetBehavior.called);1057 assert(fake1.resetBehavior.called);1058 });1059 });1060 describe(".resetHistory", function() {1061 beforeEach(function() {1062 var sandbox = (this.sandbox = createSandbox());1063 var fakes = (this.fakes = sandbox.getFakes());1064 var spy1 = sinonSpy();1065 spy1();1066 fakes.push(spy1);1067 var spy2 = sinonSpy();1068 spy2();...
60sandbox-test.js
Source:60sandbox-test.js
...69 assert.equals(typeof actual.verify, "function");70 assert.equals(typeof object.method.restore, "function");71 });72 it("adds mock to fake array", function() {73 var fakes = this.sandbox.getFakes();74 var object = {75 method: function() {76 return;77 }78 };79 var expected = this.sandbox.mock(object);80 assert(fakes.indexOf(expected) !== -1);81 });82 it("appends mocks to fake array", function() {83 var fakes = this.sandbox.getFakes();84 this.sandbox.mock({});85 this.sandbox.mock({});86 assert.equals(fakes.length, 2);87 });88 });89 describe("stub and mock test", function() {90 beforeEach(function() {91 this.sandbox = createSandbox();92 });93 it("appends mocks and stubs to fake array", function() {94 var fakes = this.sandbox.getFakes();95 this.sandbox.mock(96 {97 method: function() {98 return;99 }100 },101 "method"102 );103 this.sandbox.stub(104 {105 method: function() {106 return;107 }108 },109 "method"110 );111 assert.equals(fakes.length, 2);112 });113 });114 describe(".spy", function() {115 it("should return a spy", function() {116 var sandbox = createSandbox();117 var spy = sandbox.spy();118 assert.isFunction(spy);119 assert.equals(spy.displayName, "spy");120 });121 it("should add a spy to the internal collection", function() {122 var sandbox = createSandbox();123 var fakes = sandbox.getFakes();124 var expected;125 expected = sandbox.spy();126 assert.isTrue(fakes.indexOf(expected) !== -1);127 });128 });129 describe(".createStubInstance", function() {130 beforeEach(function() {131 this.sandbox = createSandbox();132 });133 it("stubs existing methods", function() {134 var Class = function() {135 return;136 };137 Class.prototype.method = function() {138 return;139 };140 var stub = this.sandbox.createStubInstance(Class);141 stub.method.returns(3);142 assert.equals(3, stub.method());143 });144 it("should require a function", function() {145 var sandbox = this.sandbox;146 assert.exception(147 function() {148 sandbox.createStubInstance("not a function");149 },150 {151 name: "TypeError",152 message: "The constructor should be a function."153 }154 );155 });156 it("resets all stub methods on reset()", function() {157 var Class = function() {158 return;159 };160 Class.prototype.method1 = function() {161 return;162 };163 Class.prototype.method2 = function() {164 return;165 };166 Class.prototype.method3 = function() {167 return;168 };169 var stub = this.sandbox.createStubInstance(Class);170 stub.method1.returns(1);171 stub.method2.returns(2);172 stub.method3.returns(3);173 assert.equals(3, stub.method3());174 this.sandbox.reset();175 assert.equals(undefined, stub.method1());176 assert.equals(undefined, stub.method2());177 assert.equals(undefined, stub.method3());178 });179 it("doesn't stub fake methods", function() {180 var Class = function() {181 return;182 };183 var stub = this.sandbox.createStubInstance(Class);184 assert.exception(function() {185 stub.method.returns(3);186 });187 });188 it("doesn't call the constructor", function() {189 var Class = function(a, b) {190 var c = a + b;191 throw c;192 };193 Class.prototype.method = function() {194 return;195 };196 var stub = this.sandbox.createStubInstance(Class);197 refute.exception(function() {198 stub.method(3);199 });200 });201 it("retains non function values", function() {202 var TYPE = "some-value";203 var Class = function() {204 return;205 };206 Class.prototype.type = TYPE;207 var stub = this.sandbox.createStubInstance(Class);208 assert.equals(TYPE, stub.type);209 });210 it("has no side effects on the prototype", function() {211 var proto = {212 method: function() {213 throw new Error("error");214 }215 };216 var Class = function() {217 return;218 };219 Class.prototype = proto;220 var stub = this.sandbox.createStubInstance(Class);221 refute.exception(stub.method);222 assert.exception(proto.method);223 });224 it("throws exception for non function params", function() {225 var types = [{}, 3, "hi!"];226 for (var i = 0; i < types.length; i++) {227 // yes, it's silly to create functions in a loop, it's also a test228 /* eslint-disable-next-line ie11/no-loop-func, no-loop-func */229 assert.exception(function() {230 this.sandbox.createStubInstance(types[i]);231 });232 }233 });234 });235 describe(".stub", function() {236 beforeEach(function() {237 this.sandbox = createSandbox();238 });239 it("fails if stubbing property on null", function() {240 var sandbox = this.sandbox;241 assert.exception(242 function() {243 sandbox.stub(null, "prop");244 },245 {246 message: "Trying to stub property 'prop' of null"247 }248 );249 });250 it("fails if stubbing symbol on null", function() {251 if (typeof Symbol === "function") {252 var sandbox = this.sandbox;253 assert.exception(254 function() {255 sandbox.stub(null, Symbol());256 },257 {258 message: "Trying to stub property 'Symbol()' of null"259 }260 );261 }262 });263 it("creates a stub", function() {264 var object = {265 method: function() {266 return;267 }268 };269 this.sandbox.stub(object, "method");270 assert.equals(typeof object.method.restore, "function");271 });272 it("adds stub to fake array", function() {273 var fakes = this.sandbox.getFakes();274 var object = {275 method: function() {276 return;277 }278 };279 var stub = this.sandbox.stub(object, "method");280 assert.isTrue(fakes.indexOf(stub) !== -1);281 });282 it("appends stubs to fake array", function() {283 var fakes = this.sandbox.getFakes();284 this.sandbox.stub(285 {286 method: function() {287 return;288 }289 },290 "method"291 );292 this.sandbox.stub(293 {294 method: function() {295 return;296 }297 },298 "method"299 );300 assert.equals(fakes.length, 2);301 });302 it("adds all object methods to fake array", function() {303 var fakes = this.sandbox.getFakes();304 var object = {305 method: function() {306 return;307 },308 method2: function() {309 return;310 },311 method3: function() {312 return;313 }314 };315 Object.defineProperty(object, "method3", {316 enumerable: false317 });318 this.sandbox.stub(object);319 assert.isTrue(fakes.indexOf(object.method) !== -1);320 assert.isTrue(fakes.indexOf(object.method2) !== -2);321 assert.isTrue(fakes.indexOf(object.method3) !== -3);322 assert.equals(fakes.length, 3);323 });324 it("returns a stubbed object", function() {325 var object = {326 method: function() {327 return;328 }329 };330 assert.equals(this.sandbox.stub(object), object);331 });332 it("returns a stubbed method", function() {333 var object = {334 method: function() {335 return;336 }337 };338 assert.equals(this.sandbox.stub(object, "method"), object.method);339 });340 if (typeof process !== "undefined") {341 describe("on node", function() {342 beforeEach(function() {343 process.env.HELL = "Ain't too bad";344 });345 it("stubs environment property", function() {346 var originalPrintWarning = deprecated.printWarning;347 deprecated.printWarning = function() {348 return;349 };350 this.sandbox.stub(process.env, "HELL").value("froze over");351 assert.equals(process.env.HELL, "froze over");352 deprecated.printWarning = originalPrintWarning;353 });354 });355 }356 });357 describe("stub anything", function() {358 beforeEach(function() {359 this.object = { property: 42 };360 this.sandbox = new Sandbox();361 });362 it("stubs number property", function() {363 var originalPrintWarning = deprecated.printWarning;364 deprecated.printWarning = function() {365 return;366 };367 this.sandbox.stub(this.object, "property").value(1);368 assert.equals(this.object.property, 1);369 deprecated.printWarning = originalPrintWarning;370 });371 it("restores number property", function() {372 var originalPrintWarning = deprecated.printWarning;373 deprecated.printWarning = function() {374 return;375 };376 this.sandbox.stub(this.object, "property").value(1);377 this.sandbox.restore();378 assert.equals(this.object.property, 42);379 deprecated.printWarning = originalPrintWarning;380 });381 it("fails if property does not exist", function() {382 var originalPrintWarning = deprecated.printWarning;383 deprecated.printWarning = function() {384 return;385 };386 var sandbox = this.sandbox;387 var object = {};388 assert.exception(function() {389 sandbox.stub(object, "prop", 1);390 });391 deprecated.printWarning = originalPrintWarning;392 });393 it("fails if Symbol does not exist", function() {394 if (typeof Symbol === "function") {395 var sandbox = this.sandbox;396 var object = {};397 var originalPrintWarning = deprecated.printWarning;398 deprecated.printWarning = function() {399 return;400 };401 assert.exception(402 function() {403 sandbox.stub(object, Symbol());404 },405 { message: "Cannot stub non-existent own property Symbol()" },406 TypeError407 );408 deprecated.printWarning = originalPrintWarning;409 }410 });411 });412 describe(".fake", function() {413 it("should return a fake", function() {414 var sandbox = createSandbox();415 var fake = sandbox.fake();416 assert.isFunction(fake);417 assert.equals(fake.displayName, "fake");418 });419 it("should add a fake to the internal collection", function() {420 var sandbox = createSandbox();421 var fakes = sandbox.getFakes();422 var expected;423 expected = sandbox.fake();424 assert.isTrue(fakes.indexOf(expected) !== -1);425 });426 describe(".returns", function() {427 it("should return a fake behavior", function() {428 var sandbox = createSandbox();429 var fake = sandbox.fake.returns();430 assert.isFunction(fake);431 assert.equals(fake.displayName, "fake");432 });433 it("should add a fake behavior to the internal collection", function() {434 var sandbox = createSandbox();435 var fakes = sandbox.getFakes();436 var expected;437 expected = sandbox.fake.returns();438 assert.isTrue(fakes.indexOf(expected) !== -1);439 });440 });441 describe(".throws", function() {442 it("should return a fake behavior", function() {443 var sandbox = createSandbox();444 var fake = sandbox.fake.throws();445 assert.isFunction(fake);446 assert.equals(fake.displayName, "fake");447 });448 it("should add a fake behavior to the internal collection", function() {449 var sandbox = createSandbox();450 var fakes = sandbox.getFakes();451 var expected;452 expected = sandbox.fake.throws();453 assert.isTrue(fakes.indexOf(expected) !== -1);454 });455 });456 describe(".resolves", function() {457 it("should return a fake behavior", function() {458 var sandbox = createSandbox();459 var fake = sandbox.fake.resolves();460 assert.isFunction(fake);461 assert.equals(fake.displayName, "fake");462 });463 it("should add a fake behavior to the internal collection", function() {464 var sandbox = createSandbox();465 var fakes = sandbox.getFakes();466 var expected;467 expected = sandbox.fake.resolves();468 assert.isTrue(fakes.indexOf(expected) !== -1);469 });470 });471 describe(".rejects", function() {472 it("should return a fake behavior", function() {473 var sandbox = createSandbox();474 var fake = sandbox.fake.rejects();475 assert.isFunction(fake);476 assert.equals(fake.displayName, "fake");477 });478 it("should add a fake behavior to the internal collection", function() {479 var sandbox = createSandbox();480 var fakes = sandbox.getFakes();481 var expected;482 expected = sandbox.fake.rejects();483 assert.isTrue(fakes.indexOf(expected) !== -1);484 });485 });486 describe(".yields", function() {487 it("should return a fake behavior", function() {488 var sandbox = createSandbox();489 var fake = sandbox.fake.yields();490 assert.isFunction(fake);491 assert.equals(fake.displayName, "fake");492 });493 it("should add a fake behavior to the internal collection", function() {494 var sandbox = createSandbox();495 var fakes = sandbox.getFakes();496 var expected;497 expected = sandbox.fake.yields();498 assert.isTrue(fakes.indexOf(expected) !== -1);499 });500 });501 describe(".yieldsAsync", function() {502 it("should return a fake behavior", function() {503 var sandbox = createSandbox();504 var fake = sandbox.fake.yieldsAsync();505 assert.isFunction(fake);506 assert.equals(fake.displayName, "fake");507 });508 it("should add a fake behavior to the internal collection", function() {509 var sandbox = createSandbox();510 var fakes = sandbox.getFakes();511 var expected;512 expected = sandbox.fake.yieldsAsync();513 assert.isTrue(fakes.indexOf(expected) !== -1);514 });515 });516 });517 describe(".verifyAndRestore", function() {518 beforeEach(function() {519 this.sandbox = createSandbox();520 });521 it("calls verify and restore", function() {522 this.sandbox.verify = sinonSpy();523 this.sandbox.restore = sinonSpy();524 this.sandbox.verifyAndRestore();525 assert(this.sandbox.verify.called);526 assert(this.sandbox.restore.called);527 });528 it("throws when restore throws", function() {529 this.sandbox.verify = sinonSpy();530 this.sandbox.restore = sinonStub().throws();531 assert.exception(function() {532 this.sandbox.verifyAndRestore();533 });534 });535 it("calls restore when restore throws", function() {536 var sandbox = this.sandbox;537 sandbox.verify = sinonSpy();538 sandbox.restore = sinonStub().throws();539 assert.exception(function() {540 sandbox.verifyAndRestore();541 });542 assert(sandbox.restore.called);543 });544 });545 describe(".replace", function() {546 beforeEach(function() {547 this.sandbox = createSandbox();548 });549 it("should replace a function property", function() {550 var replacement = function replacement() {551 return;552 };553 var existing = function existing() {554 return;555 };556 var object = {557 property: existing558 };559 this.sandbox.replace(object, "property", replacement);560 assert.equals(object.property, replacement);561 this.sandbox.restore();562 assert.equals(object.property, existing);563 });564 it("should replace a non-function property", function() {565 var replacement = "replacement";566 var existing = "existing";567 var object = {568 property: existing569 };570 this.sandbox.replace(object, "property", replacement);571 assert.equals(object.property, replacement);572 this.sandbox.restore();573 assert.equals(object.property, existing);574 });575 it("should replace an inherited property", function() {576 var replacement = "replacement";577 var existing = "existing";578 var object = Object.create({579 property: existing580 });581 this.sandbox.replace(object, "property", replacement);582 assert.equals(object.property, replacement);583 this.sandbox.restore();584 assert.equals(object.property, existing);585 });586 it("should error on missing descriptor", function() {587 var sandbox = this.sandbox;588 assert.exception(589 function() {590 sandbox.replace({}, "i-dont-exist");591 },592 {593 message: "Cannot replace non-existent own property i-dont-exist",594 name: "TypeError"595 }596 );597 });598 it("should error on missing replacement", function() {599 var sandbox = this.sandbox;600 var object = Object.create({601 property: "catpants"602 });603 assert.exception(604 function() {605 sandbox.replace(object, "property");606 },607 {608 message: "Expected replacement argument to be defined",609 name: "TypeError"610 }611 );612 });613 it("should refuse to replace a non-function with a function", function() {614 var sandbox = this.sandbox;615 var replacement = function() {616 return "replacement";617 };618 var existing = "existing";619 var object = {620 property: existing621 };622 assert.exception(623 function() {624 sandbox.replace(object, "property", replacement);625 },626 { message: "Cannot replace string with function" }627 );628 });629 it("should refuse to replace a function with a non-function", function() {630 var sandbox = this.sandbox;631 var replacement = "replacement";632 var object = {633 property: function() {634 return "apple pie";635 }636 };637 assert.exception(638 function() {639 sandbox.replace(object, "property", replacement);640 },641 { message: "Cannot replace function with string" }642 );643 });644 it("should refuse to replace a fake twice", function() {645 var sandbox = this.sandbox;646 var object = {647 property: function() {648 return "apple pie";649 }650 };651 sandbox.replace(object, "property", sinonFake());652 assert.exception(653 function() {654 sandbox.replace(object, "property", sinonFake());655 },656 { message: "Attempted to replace property which is already replaced" }657 );658 });659 it("should refuse to replace a string twice", function() {660 var sandbox = this.sandbox;661 var object = {662 property: "original"663 };664 sandbox.replace(object, "property", "first");665 assert.exception(666 function() {667 sandbox.replace(object, "property", "second");668 },669 { message: "Attempted to replace property which is already replaced" }670 );671 });672 it("should return the replacement argument", function() {673 var replacement = "replacement";674 var existing = "existing";675 var object = {676 property: existing677 };678 var actual = this.sandbox.replace(object, "property", replacement);679 assert.equals(actual, replacement);680 });681 describe("when asked to replace a getter", function() {682 it("should throw an Error", function() {683 var sandbox = this.sandbox;684 var object = {685 get foo() {686 return "bar";687 }688 };689 assert.exception(690 function() {691 sandbox.replace(object, "foo", sinonFake());692 },693 { message: "Use sandbox.replaceGetter for replacing getters" }694 );695 });696 });697 describe("when asked to replace a setter", function() {698 it("should throw an Error", function() {699 var sandbox = this.sandbox;700 // eslint-disable-next-line accessor-pairs701 var object = {702 set foo(value) {703 this.prop = value;704 }705 };706 assert.exception(707 function() {708 sandbox.replace(object, "foo", sinonFake());709 },710 { message: "Use sandbox.replaceSetter for replacing setters" }711 );712 });713 });714 });715 describe(".replaceGetter", function() {716 beforeEach(function() {717 this.sandbox = createSandbox();718 });719 it("should replace getters", function() {720 var expected = "baz";721 var object = {722 get foo() {723 return "bar";724 }725 };726 this.sandbox.replaceGetter(object, "foo", sinonFake.returns(expected));727 assert.equals(object.foo, expected);728 });729 it("should return replacement", function() {730 var replacement = sinonFake.returns("baz");731 var object = {732 get foo() {733 return "bar";734 }735 };736 var actual = this.sandbox.replaceGetter(object, "foo", replacement);737 assert.equals(actual, replacement);738 });739 it("should replace an inherited property", function() {740 var expected = "baz";741 var replacement = sinonFake.returns(expected);742 var existing = "existing";743 var object = Object.create({744 get foo() {745 return existing;746 }747 });748 this.sandbox.replaceGetter(object, "foo", replacement);749 assert.equals(object.foo, expected);750 this.sandbox.restore();751 assert.equals(object.foo, existing);752 });753 it("should error on missing descriptor", function() {754 var sandbox = this.sandbox;755 assert.exception(756 function() {757 sandbox.replaceGetter({}, "i-dont-exist");758 },759 {760 message: "Cannot replace non-existent own property i-dont-exist",761 name: "TypeError"762 }763 );764 });765 it("should error when descriptor has no getter", function() {766 var sandbox = this.sandbox;767 // eslint-disable-next-line accessor-pairs768 var object = {769 set catpants(_) {770 return;771 }772 };773 assert.exception(774 function() {775 sandbox.replaceGetter(object, "catpants", noop);776 },777 {778 message: "`object.property` is not a getter",779 name: "Error"780 }781 );782 });783 describe("when called with a non-function replacement argument", function() {784 it("should throw a TypeError", function() {785 var sandbox = this.sandbox;786 var expected = "baz";787 var object = {788 get foo() {789 return "bar";790 }791 };792 assert.exception(793 function() {794 sandbox.replaceGetter(object, "foo", expected);795 },796 { message: "Expected replacement argument to be a function" }797 );798 });799 });800 it("allows restoring getters", function() {801 var expected = "baz";802 var object = {803 get foo() {804 return "bar";805 }806 };807 this.sandbox.replaceGetter(object, "foo", sinonFake.returns(expected));808 this.sandbox.restore();809 assert.equals(object.foo, "bar");810 });811 it("should refuse to replace a getter twice", function() {812 var sandbox = this.sandbox;813 var object = {814 get foo() {815 return "bar";816 }817 };818 sandbox.replaceGetter(object, "foo", sinonFake.returns("one"));819 assert.exception(820 function() {821 sandbox.replaceGetter(object, "foo", sinonFake.returns("two"));822 },823 { message: "Attempted to replace foo which is already replaced" }824 );825 });826 });827 describe(".replaceSetter", function() {828 beforeEach(function() {829 this.sandbox = createSandbox();830 });831 it("should replace setter", function() {832 // eslint-disable-next-line accessor-pairs833 var object = {834 set foo(value) {835 this.prop = value;836 },837 prop: "bar"838 };839 this.sandbox.replaceSetter(object, "foo", function(val) {840 this.prop = val + "bla";841 });842 object.foo = "bla";843 assert.equals(object.prop, "blabla");844 });845 it("should return replacement", function() {846 // eslint-disable-next-line accessor-pairs847 var object = {848 set foo(value) {849 this.prop = value;850 },851 prop: "bar"852 };853 var replacement = function(val) {854 this.prop = val + "bla";855 };856 var actual = this.sandbox.replaceSetter(object, "foo", replacement);857 assert.equals(actual, replacement);858 });859 it("should replace an inherited property", function() {860 // eslint-disable-next-line accessor-pairs861 var object = Object.create({862 set foo(value) {863 this.prop = value;864 },865 prop: "bar"866 });867 var replacement = function(value) {868 this.prop = value + "blabla";869 };870 this.sandbox.replaceSetter(object, "foo", replacement);871 object.foo = "doodle";872 assert.equals(object.prop, "doodleblabla");873 this.sandbox.restore();874 object.foo = "doodle";875 assert.equals(object.prop, "doodle");876 });877 it("should error on missing descriptor", function() {878 var sandbox = this.sandbox;879 assert.exception(880 function() {881 sandbox.replaceSetter({}, "i-dont-exist");882 },883 {884 message: "Cannot replace non-existent own property i-dont-exist",885 name: "TypeError"886 }887 );888 });889 it("should error when descriptor has no setter", function() {890 var sandbox = this.sandbox;891 var object = {892 get catpants() {893 return;894 }895 };896 assert.exception(897 function() {898 sandbox.replaceSetter(object, "catpants", noop);899 },900 {901 message: "`object.property` is not a setter",902 name: "Error"903 }904 );905 });906 describe("when called with a non-function replacement argument", function() {907 it("should throw a TypeError", function() {908 var sandbox = this.sandbox;909 // eslint-disable-next-line accessor-pairs910 var object = {911 set foo(value) {912 this.prop = value;913 },914 prop: "bar"915 };916 assert.exception(917 function() {918 sandbox.replaceSetter(object, "foo", "bla");919 },920 { message: "Expected replacement argument to be a function" }921 );922 });923 });924 it("allows restoring setters", function() {925 // eslint-disable-next-line accessor-pairs926 var object = {927 set foo(value) {928 this.prop = value;929 },930 prop: "bar"931 };932 this.sandbox.replaceSetter(object, "foo", function(val) {933 this.prop = val + "bla";934 });935 this.sandbox.restore();936 object.prop = "bla";937 assert.equals(object.prop, "bla");938 });939 it("should refuse to replace a setter twice", function() {940 var sandbox = this.sandbox;941 // eslint-disable-next-line accessor-pairs942 var object = {943 set foo(value) {944 return;945 }946 };947 sandbox.replaceSetter(object, "foo", sinonFake());948 assert.exception(949 function() {950 sandbox.replaceSetter(object, "foo", sinonFake.returns("two"));951 },952 { message: "Attempted to replace foo which is already replaced" }953 );954 });955 });956 describe(".reset", function() {957 beforeEach(function() {958 var sandbox = (this.sandbox = createSandbox());959 var fakes = sandbox.getFakes();960 fakes.push({961 reset: sinonSpy(),962 resetHistory: sinonSpy()963 });964 fakes.push({965 reset: sinonSpy(),966 resetHistory: sinonSpy()967 });968 });969 it("calls reset on all fakes", function() {970 var fake0 = this.sandbox.getFakes()[0];971 var fake1 = this.sandbox.getFakes()[1];972 this.sandbox.reset();973 assert(fake0.reset.called);974 assert(fake1.reset.called);975 });976 it("calls resetHistory on all fakes", function() {977 var fake0 = this.sandbox.getFakes()[0];978 var fake1 = this.sandbox.getFakes()[1];979 this.sandbox.reset();980 assert(fake0.resetHistory.called);981 assert(fake1.resetHistory.called);982 });983 it("resets fake behaviours", function() {984 var fake = this.sandbox.fake();985 fake(1234);986 this.sandbox.reset();987 assert.equals(fake.getCalls(), []);988 });989 });990 describe(".resetBehavior", function() {991 beforeEach(function() {992 var sandbox = (this.sandbox = createSandbox());993 var fakes = sandbox.getFakes();994 fakes.push({ resetBehavior: sinonSpy() });995 fakes.push({ resetBehavior: sinonSpy() });996 });997 it("calls resetBehavior on all fakes", function() {998 var fake0 = this.sandbox.getFakes()[0];999 var fake1 = this.sandbox.getFakes()[1];1000 this.sandbox.resetBehavior();1001 assert(fake0.resetBehavior.called);1002 assert(fake1.resetBehavior.called);1003 });1004 });1005 describe(".resetHistory", function() {1006 beforeEach(function() {1007 var sandbox = (this.sandbox = createSandbox());1008 var fakes = (this.fakes = sandbox.getFakes());1009 var spy1 = sinonSpy();1010 spy1();1011 fakes.push(spy1);1012 var spy2 = sinonSpy();1013 spy2();...
Using AI Code Generation
1var sinon = require('sinon');2var chai = require('chai');3var assert = chai.assert;4var expect = chai.expect;5var should = chai.should();6var sinonChai = require('sinon-chai');7chai.use(sinonChai);8describe('sinon sandbox', function () {9 it('should return an array of all the fakes created', function () {10 var sandbox = sinon.sandbox.create();11 var fake = sandbox.stub();12 var fake2 = sandbox.stub();13 var fakes = sandbox.getFakes();14 expect(fakes).to.be.an('array');15 expect(fakes).to.have.length(2);16 expect(fakes).to.contain(fake);17 expect(fakes).to.contain(fake2);18 });19});
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3describe('sinon', function() {4 var sandbox;5 beforeEach(function() {6 sandbox = sinon.sandbox.create();7 });8 afterEach(function() {9 sandbox.restore();10 });11 it('should create a sandbox', function() {12 var spy = sandbox.spy();13 spy();14 assert(spy.called);15 });16 it('should get all the fakes', function() {17 var spy = sandbox.spy();18 var stub = sandbox.stub();19 var mock = sandbox.mock();20 var clock = sandbox.useFakeTimers();21 var server = sandbox.useFakeServer();22 var request = sandbox.useFakeXMLHttpRequest();23 var fakes = sandbox.getFakes();24 assert.equal(fakes.length, 5);25 });26});
Using AI Code Generation
1var sinon = require('sinon');2describe('test', function() {3 var clock;4 beforeEach(function() {5 clock = sinon.useFakeTimers();6 });7 afterEach(function() {8 clock.restore();9 });10 it('test', function() {11 assert.equal(this.sandbox.getFakes().length, 1);12 });13});
Using AI Code Generation
1describe('Test', function () {2 beforeEach(function () {3 this.sandbox = sinon.sandbox.create();4 });5 afterEach(function () {6 this.sandbox.restore();7 });8 it('should return a fake', function () {9 var fake = this.sandbox.getFakes()[0];10 assert(fake);11 });12});
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('chai').assert;3describe('sinon sandbox', function () {4 var sandbox;5 beforeEach(function () {6 sandbox = sinon.sandbox.create();7 });8 afterEach(function () {9 sandbox.restore();10 });11 it('should call callback', function () {12 var callback = sandbox.spy();13 callback();14 assert(callback.calledOnce);15 });16 it('should call callback', function () {17 var callback = sandbox.spy();18 callback();19 assert(callback.calledOnce);20 });21 it('should call callback', function () {22 var callback = sandbox.spy();23 callback();24 assert(callback.calledOnce);25 });26 it('should call callback', function () {27 var callback = sandbox.spy();28 callback();29 assert(callback.calledOnce);30 });31 it('should call callback', function () {32 var callback = sandbox.spy();33 callback();34 assert(callback.calledOnce);35 });36});
Using AI Code Generation
1describe('test', function() {2 var sandbox;3 beforeEach(function() {4 sandbox = sinon.sandbox.create();5 });6 afterEach(function() {7 sandbox.restore();8 });9 it('should call the callback with the correct value', function() {10 var callback = sandbox.spy();11 callback(42);12 assert(callback.calledWith(42));13 });14});
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var myModule = require('./myModule.js');4var myModule = require('./myModule.js');5var myModule = require('./myModule.js');6describe('myModule', function () {7 beforeEach(function () {8 this.sandbox = sinon.sandbox.create();9 });10 afterEach(function () {11 this.sandbox.restore();12 });13 it('should call the callback', function () {14 var callback = this.sandbox.spy();15 myModule(callback);16 assert(callback.called);17 });18});
Using AI Code Generation
1var sinon = require('sinon');2var sandbox = sinon.sandbox.create();3var fake = sandbox.fake();4var stub = sandbox.stub();5var spy = sandbox.spy();6var mock = sandbox.mock();7var obj = sandbox.mock();8var obj2 = sandbox.mock();9var obj3 = sandbox.mock();10var obj4 = sandbox.mock();11var obj5 = sandbox.mock();12var obj6 = sandbox.mock();13var obj7 = sandbox.mock();14var obj8 = sandbox.mock();15var obj9 = sandbox.mock();16var obj10 = sandbox.mock();17var obj11 = sandbox.mock();18var obj12 = sandbox.mock();19var obj13 = sandbox.mock();20var obj14 = sandbox.mock();21var obj15 = sandbox.mock();22var obj16 = sandbox.mock();23var obj17 = sandbox.mock();24var obj18 = sandbox.mock();25var obj19 = sandbox.mock();26var obj20 = sandbox.mock();27var obj21 = sandbox.mock();28var obj22 = sandbox.mock();29var obj23 = sandbox.mock();30var obj24 = sandbox.mock();31var obj25 = sandbox.mock();32var obj26 = sandbox.mock();33var obj27 = sandbox.mock();34var obj28 = sandbox.mock();35var obj29 = sandbox.mock();36var obj30 = sandbox.mock();37var obj31 = sandbox.mock();38var obj32 = sandbox.mock();39var obj33 = sandbox.mock();40var obj34 = sandbox.mock();41var obj35 = sandbox.mock();42var obj36 = sandbox.mock();43var obj37 = sandbox.mock();44var obj38 = sandbox.mock();45var obj39 = sandbox.mock();46var obj40 = sandbox.mock();47var obj41 = sandbox.mock();48var obj42 = sandbox.mock();49var obj43 = sandbox.mock();50var obj44 = sandbox.mock();51var obj45 = sandbox.mock();52var obj46 = sandbox.mock();53var obj47 = sandbox.mock();54var obj48 = sandbox.mock();55var obj49 = sandbox.mock();56var obj50 = sandbox.mock();57var obj51 = sandbox.mock();58var obj52 = sandbox.mock();59var obj53 = sandbox.mock();60var obj54 = sandbox.mock();61var obj55 = sandbox.mock();62var obj56 = sandbox.mock();63var obj57 = sandbox.mock();64var obj58 = sandbox.mock();65var obj59 = sandbox.mock();66var obj60 = sandbox.mock();67var obj61 = sandbox.mock();68var obj62 = sandbox.mock();69var obj63 = sandbox.mock();70var obj64 = sandbox.mock();
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!