How to use sinonMock method in sinon

Best JavaScript code snippet using sinon

mock-test.js

Source:mock-test.js Github

copy

Full Screen

...8var assert = referee.assert;9var refute = referee.refute;10describe("sinonMock", function() {11 it("creates anonymous mock functions", function() {12 var expectation = sinonMock();13 assert.equals(expectation.method, "Anonymous mock");14 });15 it("creates named anonymous mock functions", function() {16 var expectation = sinonMock("functionName");17 assert.equals(expectation.method, "functionName");18 });19 describe(".create", function() {20 it("returns function with expects method", function() {21 var mock = sinonMock.create({});22 assert.isFunction(mock.expects);23 });24 it("throws without object", function() {25 assert.exception(26 function() {27 sinonMock.create();28 },29 { name: "TypeError" }30 );31 });32 });33 describe(".expects", function() {34 beforeEach(function() {35 this.mock = sinonMock.create({36 someMethod: function() {37 return;38 }39 });40 });41 it("throws without method", function() {42 var mock = this.mock;43 assert.exception(44 function() {45 mock.expects();46 },47 { name: "TypeError" }48 );49 });50 it("returns expectation", function() {51 var result = this.mock.expects("someMethod");52 assert.isFunction(result);53 assert.equals(result.method, "someMethod");54 });55 it("throws if expecting a non-existent method", function() {56 var mock = this.mock;57 assert.exception(function() {58 mock.expects("someMethod2");59 });60 });61 });62 describe(".expectation", function() {63 beforeEach(function() {64 this.method = "myMeth";65 this.expectation = sinonExpectation.create(this.method);66 });67 it("creates unnamed expectation", function() {68 var anonMock = sinonExpectation.create();69 anonMock.never();70 assert(anonMock.verify());71 });72 it("uses 'anonymous mock expectation' for unnamed expectation", function() {73 var anonMock = sinonExpectation.create();74 anonMock.once();75 assert.exception(76 function() {77 anonMock.verify();78 },79 {80 message: "anonymous mock expectation"81 }82 );83 });84 it("call expectation", function() {85 this.expectation();86 assert.isFunction(this.expectation.invoke);87 assert(this.expectation.called);88 });89 it("is invokable", function() {90 var expectation = this.expectation;91 refute.exception(function() {92 expectation();93 });94 });95 describe(".returns", function() {96 it("returns configured return value", function() {97 var object = {};98 this.expectation.returns(object);99 assert.same(this.expectation(), object);100 });101 });102 describe("call", function() {103 it("is called with correct this value", function() {104 var object = { method: this.expectation };105 object.method();106 assert(this.expectation.calledOn(object));107 });108 });109 describe(".callCount", function() {110 it("onlys be invokable once by default", function() {111 var expectation = this.expectation;112 expectation();113 assert.exception(114 function() {115 expectation();116 },117 { name: "ExpectationError" }118 );119 });120 it("throw readable error", function() {121 var expectation = this.expectation;122 expectation();123 assert.exception(expectation, {124 message: "myMeth already called once"125 });126 });127 });128 describe(".callCountNever", function() {129 it("is not callable", function() {130 var expectation = this.expectation;131 expectation.never();132 assert.exception(133 function() {134 expectation();135 },136 { name: "ExpectationError" }137 );138 });139 it("returns expectation for chaining", function() {140 assert.same(this.expectation.never(), this.expectation);141 });142 });143 describe(".callCountOnce", function() {144 it("allows one call", function() {145 var expectation = this.expectation;146 expectation.once();147 expectation();148 assert.exception(149 function() {150 expectation();151 },152 { name: "ExpectationError" }153 );154 });155 it("returns expectation for chaining", function() {156 assert.same(this.expectation.once(), this.expectation);157 });158 });159 describe(".callCountTwice", function() {160 it("allows two calls", function() {161 var expectation = this.expectation;162 expectation.twice();163 expectation();164 expectation();165 assert.exception(166 function() {167 expectation();168 },169 { name: "ExpectationError" }170 );171 });172 it("returns expectation for chaining", function() {173 assert.same(this.expectation.twice(), this.expectation);174 });175 });176 describe(".callCountThrice", function() {177 it("allows three calls", function() {178 var expectation = this.expectation;179 expectation.thrice();180 expectation();181 expectation();182 expectation();183 assert.exception(184 function() {185 expectation();186 },187 { name: "ExpectationError" }188 );189 });190 it("returns expectation for chaining", function() {191 assert.same(this.expectation.thrice(), this.expectation);192 });193 });194 describe(".callCountExactly", function() {195 it("allows specified number of calls", function() {196 var expectation = this.expectation;197 expectation.exactly(2);198 expectation();199 expectation();200 assert.exception(201 function() {202 expectation();203 },204 { name: "ExpectationError" }205 );206 });207 it("returns expectation for chaining", function() {208 assert.same(this.expectation.exactly(2), this.expectation);209 });210 it("throws without argument", function() {211 var expectation = this.expectation;212 assert.exception(213 function() {214 expectation.exactly();215 },216 { name: "TypeError" }217 );218 });219 it("throws without number", function() {220 var expectation = this.expectation;221 assert.exception(222 function() {223 expectation.exactly("12");224 },225 { name: "TypeError" }226 );227 });228 it("throws with Symbol", function() {229 if (typeof Symbol === "function") {230 var expectation = this.expectation;231 assert.exception(232 function() {233 expectation.exactly(Symbol());234 },235 function(err) {236 return err.message === "'Symbol()' is not a number";237 }238 );239 }240 });241 });242 describe(".atLeast", function() {243 it("throws without argument", function() {244 var expectation = this.expectation;245 assert.exception(246 function() {247 expectation.atLeast();248 },249 { name: "TypeError" }250 );251 });252 it("throws without number", function() {253 var expectation = this.expectation;254 assert.exception(255 function() {256 expectation.atLeast({});257 },258 { name: "TypeError" }259 );260 });261 it("throws with Symbol", function() {262 if (typeof Symbol === "function") {263 var expectation = this.expectation;264 assert.exception(265 function() {266 expectation.atLeast(Symbol());267 },268 function(err) {269 return err.message === "'Symbol()' is not number";270 }271 );272 }273 });274 it("returns expectation for chaining", function() {275 assert.same(this.expectation.atLeast(2), this.expectation);276 });277 it("allows any number of calls", function() {278 var expectation = this.expectation;279 expectation.atLeast(2);280 expectation();281 expectation();282 refute.exception(function() {283 expectation();284 expectation();285 });286 });287 it("should not be met with too few calls", function() {288 this.expectation.atLeast(2);289 this.expectation();290 assert.isFalse(this.expectation.met());291 });292 it("is met with exact calls", function() {293 this.expectation.atLeast(2);294 this.expectation();295 this.expectation();296 assert(this.expectation.met());297 });298 it("is met with excessive calls", function() {299 this.expectation.atLeast(2);300 this.expectation();301 this.expectation();302 this.expectation();303 assert(this.expectation.met());304 });305 it("should not throw when exceeding at least expectation", function() {306 var obj = {307 foobar: function() {308 return;309 }310 };311 var mock = sinonMock(obj);312 mock.expects("foobar").atLeast(1);313 obj.foobar();314 refute.exception(function() {315 obj.foobar();316 mock.verify();317 });318 });319 it("should not throw when exceeding at least expectation and withargs", function() {320 var obj = {321 foobar: function() {322 return;323 }324 };325 var mock = sinonMock(obj);326 mock.expects("foobar").withArgs("arg1");327 mock.expects("foobar")328 .atLeast(1)329 .withArgs("arg2");330 obj.foobar("arg1");331 obj.foobar("arg2");332 obj.foobar("arg2");333 assert(mock.verify());334 });335 });336 describe(".atMost", function() {337 it("throws without argument", function() {338 var expectation = this.expectation;339 assert.exception(340 function() {341 expectation.atMost();342 },343 { name: "TypeError" }344 );345 });346 it("throws without number", function() {347 var expectation = this.expectation;348 assert.exception(349 function() {350 expectation.atMost({});351 },352 { name: "TypeError" }353 );354 });355 it("throws with Symbol", function() {356 if (typeof Symbol === "function") {357 var expectation = this.expectation;358 assert.exception(359 function() {360 expectation.atMost(Symbol());361 },362 function(err) {363 return err.message === "'Symbol()' is not number";364 }365 );366 }367 });368 it("returns expectation for chaining", function() {369 assert.same(this.expectation.atMost(2), this.expectation);370 });371 it("allows fewer calls", function() {372 var expectation = this.expectation;373 expectation.atMost(2);374 refute.exception(function() {375 expectation();376 });377 });378 it("is met with fewer calls", function() {379 this.expectation.atMost(2);380 this.expectation();381 assert(this.expectation.met());382 });383 it("is met with exact calls", function() {384 this.expectation.atMost(2);385 this.expectation();386 this.expectation();387 assert(this.expectation.met());388 });389 it("should not be met with excessive calls", function() {390 var expectation = this.expectation;391 this.expectation.atMost(2);392 this.expectation();393 this.expectation();394 assert.exception(395 function() {396 expectation();397 },398 { name: "ExpectationError" }399 );400 assert.isFalse(this.expectation.met());401 });402 });403 describe(".atMostAndAtLeast", function() {404 beforeEach(function() {405 this.expectation.atLeast(2);406 this.expectation.atMost(3);407 });408 it("should not be met with too few calls", function() {409 this.expectation();410 assert.isFalse(this.expectation.met());411 });412 it("is met with minimum calls", function() {413 this.expectation();414 this.expectation();415 assert(this.expectation.met());416 });417 it("is met with maximum calls", function() {418 this.expectation();419 this.expectation();420 this.expectation();421 assert(this.expectation.met());422 });423 it("throws with excessive calls", function() {424 var expectation = this.expectation;425 expectation();426 expectation();427 expectation();428 assert.exception(429 function() {430 expectation();431 },432 { name: "ExpectationError" }433 );434 });435 });436 describe(".met", function() {437 it("should not be met when not called enough times", function() {438 assert.isFalse(this.expectation.met());439 });440 it("is met when called enough times", function() {441 this.expectation();442 assert(this.expectation.met());443 });444 it("should not be met when called too many times", function() {445 this.expectation();446 assert.exception(this.expectation);447 assert.isFalse(this.expectation.met());448 });449 });450 describe(".withArgs", function() {451 var expectedException = function(name) {452 return {453 test: function(actual) {454 return actual.name === name;455 },456 toString: function() {457 return name;458 }459 };460 };461 it("returns expectation for chaining", function() {462 assert.same(this.expectation.withArgs(1), this.expectation);463 });464 it("accepts call with expected args", function() {465 this.expectation.withArgs(1, 2, 3);466 this.expectation(1, 2, 3);467 assert(this.expectation.met());468 });469 it("throws when called without args", function() {470 var expectation = this.expectation;471 expectation.withArgs(1, 2, 3);472 assert.exception(473 function() {474 expectation();475 },476 { name: "ExpectationError" }477 );478 });479 it("throws when called with too few args", function() {480 var expectation = this.expectation;481 expectation.withArgs(1, 2, 3);482 assert.exception(483 function() {484 expectation(1, 2);485 },486 { name: "ExpectationError" }487 );488 });489 it("throws when called with wrong args", function() {490 var expectation = this.expectation;491 expectation.withArgs(1, 2, 3);492 assert.exception(function() {493 expectation(2, 2, 3);494 }, expectedException("ExpectationError"));495 });496 it("allows excessive args", function() {497 var expectation = this.expectation;498 expectation.withArgs(1, 2, 3);499 refute.exception(function() {500 expectation(1, 2, 3, 4);501 });502 });503 it("calls accept with no args", function() {504 this.expectation.withArgs();505 this.expectation();506 assert(this.expectation.met());507 });508 it("allows no args called with excessive args", function() {509 var expectation = this.expectation;510 expectation.withArgs();511 refute.exception(function() {512 expectation(1, 2, 3);513 });514 });515 it("works with sinon matchers", function() {516 this.expectation.withArgs(match.number, match.string, match.func);517 this.expectation(1, "test", function() {518 return;519 });520 assert(this.expectation.met());521 });522 it("throws when sinon matchers fail", function() {523 var expectation = this.expectation;524 this.expectation.withArgs(match.number, match.string, match.func);525 assert.exception(526 function() {527 expectation(1, 2, 3);528 },529 { name: "ExpectationError" }530 );531 });532 it("should not throw when expectation withArgs using matcher", function() {533 var obj = {534 foobar: function() {535 return;536 }537 };538 var mock = sinonMock(obj);539 mock.expects("foobar").withArgs(match.string);540 refute.exception(function() {541 obj.foobar("arg1");542 });543 });544 });545 describe(".withExactArgs", function() {546 it("returns expectation for chaining", function() {547 assert.same(this.expectation.withExactArgs(1), this.expectation);548 });549 it("accepts call with expected args", function() {550 this.expectation.withExactArgs(1, 2, 3);551 this.expectation(1, 2, 3);552 assert(this.expectation.met());553 });554 it("throws when called without args", function() {555 var expectation = this.expectation;556 expectation.withExactArgs(1, 2, 3);557 assert.exception(558 function() {559 expectation();560 },561 { name: "ExpectationError" }562 );563 });564 it("throws when called with too few args", function() {565 var expectation = this.expectation;566 expectation.withExactArgs(1, 2, 3);567 assert.exception(568 function() {569 expectation(1, 2);570 },571 { name: "ExpectationError" }572 );573 });574 it("throws when called with wrong args", function() {575 var expectation = this.expectation;576 expectation.withExactArgs(1, 2, 3);577 assert.exception(578 function() {579 expectation(2, 2, 3);580 },581 { name: "ExpectationError" }582 );583 });584 it("should not allow excessive args", function() {585 var expectation = this.expectation;586 expectation.withExactArgs(1, 2, 3);587 assert.exception(588 function() {589 expectation(1, 2, 3, 4);590 },591 { name: "ExpectationError" }592 );593 });594 it("accepts call with no expected args", function() {595 this.expectation.withExactArgs();596 this.expectation();597 assert(this.expectation.met());598 });599 it("does not allow excessive args with no expected args", function() {600 var expectation = this.expectation;601 expectation.withExactArgs();602 assert.exception(603 function() {604 expectation(1, 2, 3);605 },606 { name: "ExpectationError" }607 );608 });609 });610 describe(".on", function() {611 it("returns expectation for chaining", function() {612 assert.same(this.expectation.on({}), this.expectation);613 });614 it("allows calls on object", function() {615 this.expectation.on(this);616 this.expectation();617 assert(this.expectation.met());618 });619 it("throws if called on wrong object", function() {620 var expectation = this.expectation;621 expectation.on({});622 assert.exception(623 function() {624 expectation();625 },626 { name: "ExpectationError" }627 );628 });629 it("throws if calls on wrong Symbol", function() {630 if (typeof Symbol === "function") {631 var expectation = sinonExpectation.create("method");632 expectation.on(Symbol());633 assert.exception(634 function() {635 expectation.call(Symbol());636 },637 function(err) {638 return err.message === "method called with Symbol() as thisValue, expected Symbol()";639 }640 );641 }642 });643 });644 describe(".verify", function() {645 it("pass if met", function() {646 sinonStub(sinonExpectation, "pass");647 var expectation = this.expectation;648 expectation();649 expectation.verify();650 assert.equals(sinonExpectation.pass.callCount, 1);651 sinonExpectation.pass.restore();652 });653 it("throws if not called enough times", function() {654 var expectation = this.expectation;655 assert.exception(656 function() {657 expectation.verify();658 },659 { name: "ExpectationError" }660 );661 });662 it("throws readable error", function() {663 var expectation = this.expectation;664 assert.exception(665 function() {666 expectation.verify();667 },668 {669 message: "Expected myMeth([...]) once (never called)"670 }671 );672 });673 });674 });675 describe(".verify", function() {676 beforeEach(function() {677 this.method = function() {678 return;679 };680 this.object = { method: this.method };681 this.mock = sinonMock.create(this.object);682 });683 it("restores mocks", function() {684 this.object.method();685 this.object.method.call(this.thisValue);686 this.mock.verify();687 assert.same(this.object.method, this.method);688 });689 it("passes verified mocks", function() {690 sinonStub(sinonExpectation, "pass");691 this.mock.expects("method").once();692 this.object.method();693 this.mock.verify();694 assert.equals(sinonExpectation.pass.callCount, 1);695 sinonExpectation.pass.restore();696 });697 it("restores if not met", function() {698 var mock = this.mock;699 mock.expects("method");700 assert.exception(701 function() {702 mock.verify();703 },704 { name: "ExpectationError" }705 );706 assert.same(this.object.method, this.method);707 });708 it("includes all calls in error message", function() {709 var mock = this.mock;710 mock.expects("method").thrice();711 mock.expects("method")712 .once()713 .withArgs(42);714 assert.exception(715 function() {716 mock.verify();717 },718 {719 message:720 "Expected method([...]) thrice (never called)\n" +721 "Expected method(42[, ...]) once (never called)"722 }723 );724 });725 it("includes exact expected arguments in error message", function() {726 var mock = this.mock;727 mock.expects("method")728 .once()729 .withExactArgs(42);730 assert.exception(731 function() {732 mock.verify();733 },734 {735 message: "Expected method(42) once (never called)"736 }737 );738 });739 it("includes received call count in error message", function() {740 var mock = this.mock;741 mock.expects("method")742 .thrice()743 .withExactArgs(42);744 this.object.method(42);745 assert.exception(746 function() {747 mock.verify();748 },749 {750 message: "Expected method(42) thrice (called once)"751 }752 );753 });754 it("includes unexpected calls in error message", function() {755 var mock = this.mock;756 var object = this.object;757 mock.expects("method")758 .thrice()759 .withExactArgs(42);760 assert.exception(761 function() {762 object.method();763 },764 {765 message: "Unexpected call: method()\n Expected method(42) thrice (never called)"766 }767 );768 });769 it("includes met expectations in error message", function() {770 var mock = this.mock;771 var object = this.object;772 mock.expects("method")773 .once()774 .withArgs(1);775 mock.expects("method")776 .thrice()777 .withExactArgs(42);778 object.method(1);779 assert.exception(780 function() {781 object.method();782 },783 {784 message:785 "Unexpected call: method()\n" +786 " Expectation met: method(1[, ...]) once\n" +787 " Expected method(42) thrice (never called)"788 }789 );790 });791 it("includes met expectations in error message from verify", function() {792 var mock = this.mock;793 mock.expects("method")794 .once()795 .withArgs(1);796 mock.expects("method")797 .thrice()798 .withExactArgs(42);799 this.object.method(1);800 assert.exception(801 function() {802 mock.verify();803 },804 {805 message: "Expected method(42) thrice (never called)\nExpectation met: method(1[, ...]) once"806 }807 );808 });809 it("reports min calls in error message", function() {810 var mock = this.mock;811 mock.expects("method").atLeast(1);812 assert.exception(813 function() {814 mock.verify();815 },816 {817 message: "Expected method([...]) at least once (never called)"818 }819 );820 });821 it("reports max calls in error message", function() {822 var mock = this.mock;823 var object = this.object;824 mock.expects("method").atMost(2);825 assert.exception(826 function() {827 object.method();828 object.method();829 object.method();830 },831 {832 message: "Unexpected call: method()\n Expectation met: method([...]) at most twice"833 }834 );835 });836 it("reports min calls in met expectation", function() {837 var mock = this.mock;838 var object = this.object;839 mock.expects("method").atLeast(1);840 mock.expects("method")841 .withArgs(2)842 .once();843 assert.exception(844 function() {845 object.method();846 object.method(2);847 object.method(2);848 },849 {850 message:851 "Unexpected call: method(2)\n" +852 " Expectation met: method([...]) at least once\n" +853 " Expectation met: method(2[, ...]) once"854 }855 );856 });857 it("reports max and min calls in error messages", function() {858 var mock = this.mock;859 mock.expects("method")860 .atLeast(1)861 .atMost(2);862 assert.exception(863 function() {864 mock.verify();865 },866 {867 message: "Expected method([...]) at least once and at most twice (never called)"868 }869 );870 });871 it("fails even if the original expectation exception was caught", function() {872 var mock = this.mock;873 var object = this.object;874 mock.expects("method").once();875 object.method();876 assert.exception(function() {877 object.method();878 });879 assert.exception(880 function() {881 mock.verify();882 },883 { name: "ExpectationError" }884 );885 });886 it("does not call pass if no expectations", function() {887 var pass = sinonStub(sinonExpectation, "pass");888 var mock = this.mock;889 mock.expects("method").never();890 delete mock.expectations;891 mock.verify();892 refute(pass.called, "expectation.pass should not be called");893 pass.restore();894 });895 });896 describe(".usingPromise", function() {897 beforeEach(function() {898 this.method = function() {899 return;900 };901 this.object = { method: this.method };902 this.mock = sinonMock.create(this.object);903 });904 it("must be a function", function() {905 assert.isFunction(this.mock.usingPromise);906 });907 it("must return the mock", function() {908 var mockPromise = {};909 var actual = this.mock.usingPromise(mockPromise);910 assert.same(actual, this.mock);911 });912 it("must set all expectations with mockPromise", function() {913 if (!global.Promise) {914 return this.skip();915 }916 var resolveValue = {};917 var mockPromise = {918 resolve: sinonStub.create().resolves(resolveValue)919 };920 this.mock.usingPromise(mockPromise);921 this.mock.expects("method").resolves({});922 return this.object.method().then(function(action) {923 assert.same(resolveValue, action);924 assert(mockPromise.resolve.calledOnce);925 });926 });927 });928 describe("mock object", function() {929 beforeEach(function() {930 this.method = function() {931 return;932 };933 this.object = { method: this.method };934 this.mock = sinonMock.create(this.object);935 });936 it("mocks object method", function() {937 this.mock.expects("method");938 assert.isFunction(this.object.method);939 refute.same(this.object.method, this.method);940 });941 it("reverts mocked method", function() {942 this.mock.expects("method");943 this.object.method.restore();944 assert.same(this.object.method, this.method);945 });946 it("reverts expectation", function() {947 this.mock.expects("method");948 this.object.method.restore();949 assert.same(this.object.method, this.method);950 });951 it("reverts mock", function() {952 this.mock.expects("method");953 this.mock.restore();954 assert.same(this.object.method, this.method);955 });956 it("verifies mock", function() {957 this.mock.expects("method");958 this.object.method();959 var mock = this.mock;960 refute.exception(function() {961 assert(mock.verify());962 });963 });964 it("verifies mock with unmet expectations", function() {965 this.mock.expects("method");966 var mock = this.mock;967 assert.exception(968 function() {969 assert(mock.verify());970 },971 { name: "ExpectationError" }972 );973 });974 });975 describe("mock method multiple times", function() {976 beforeEach(function() {977 this.thisValue = {};978 this.method = function() {979 return;980 };981 this.object = { method: this.method };982 this.mock = sinonMock.create(this.object);983 this.mock.expects("method");984 this.mock.expects("method").on(this.thisValue);985 });986 it("queues expectations", function() {987 var object = this.object;988 refute.exception(function() {989 object.method();990 });991 });992 it("starts on next expectation when first is met", function() {993 var object = this.object;994 object.method();995 assert.exception(996 function() {997 object.method();998 },999 { name: "ExpectationError" }1000 );1001 });1002 it("fails on last expectation", function() {1003 var object = this.object;1004 object.method();1005 object.method.call(this.thisValue);1006 assert.exception(1007 function() {1008 object.method();1009 },1010 { name: "ExpectationError" }1011 );1012 });1013 it("allows mock calls in any order", function() {1014 var object = {1015 method: function() {1016 return;1017 }1018 };1019 var mock = sinonMock(object);1020 mock.expects("method")1021 .once()1022 .withArgs(42);1023 mock.expects("method")1024 .twice()1025 .withArgs("Yeah");1026 refute.exception(function() {1027 object.method("Yeah");1028 });1029 refute.exception(function() {1030 object.method(42);1031 });1032 assert.exception(function() {1033 object.method(1);1034 });1035 refute.exception(function() {1036 object.method("Yeah");1037 });1038 assert.exception(function() {1039 object.method(42);1040 });1041 });1042 });1043 describe("mock function", function() {1044 it("returns mock method", function() {1045 var mock = sinonMock();1046 assert.isFunction(mock);1047 assert.isFunction(mock.atLeast);1048 assert.isFunction(mock.verify);1049 });1050 it("returns mock object", function() {1051 var mock = sinonMock({});1052 assert.isObject(mock);1053 assert.isFunction(mock.expects);1054 assert.isFunction(mock.verify);1055 });1056 });1057 describe(".yields", function() {1058 it("invokes only argument as callback", function() {1059 var mock = sinonMock().yields();1060 var spy = sinonSpy();1061 mock(spy);1062 assert(spy.calledOnce);1063 assert.equals(spy.args[0].length, 0);1064 });1065 it("throws understandable error if no callback is passed", function() {1066 var mock = sinonMock().yields();1067 assert.exception(mock, {1068 message: "stub expected to yield, but no callback was passed."1069 });1070 });1071 });...

Full Screen

Full Screen

SinonMatcher.js

Source:SinonMatcher.js Github

copy

Full Screen

1'use strict';2const isFunction = require('lodash/isFunction');3const create = require('lodash/create');4const size = require('lodash/size');5const isArray = require('lodash/isArray');6const {TypeSafeMatcher} = require('hamjest');7const getType = require('hamjest/lib/utils/getType');8function SinonMatcher() {9 function isSinonMock(sinonMock) {10 return isFunction(sinonMock) && isArray(sinonMock.args);11 }12 function getCallCount(sinonMock) {13 return sinonMock && size(sinonMock.args) || 0;14 }15 return create(new TypeSafeMatcher(), {16 getCallCount: getCallCount,17 isExpectedType: isSinonMock,18 describeMismatch: function (actual, description) {19 if (!this.isExpectedType(actual)) {20 if (!actual) {21 description.append('was ')22 .appendValue(actual);23 return;24 }25 if (isFunction(actual)) {26 description27 .append('was a ')28 .append(getType(actual))29 .append(' without a mock');30 return;31 }32 description33 .append('was a ')34 .append(getType(actual))35 .append(' (')36 .appendValue(actual)37 .append(')');38 } else {39 return this.describeMismatchSafely(actual, description);40 }41 },42 });43}...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var chai = require('chai');3var sinonChai = require('sinon-chai');4var expect = chai.expect;5chai.use(sinonChai);6var mock = sinon.mock;7var stub = sinon.stub;8var spy = sinon.spy;9var assert = chai.assert;10var should = chai.should;11var sinonTest = require('sinon-test');12sinon.test = sinonTest.configureTest(sinon);13var test = require('../src/test.js');14describe('Test', function() {15 it('should call the callback', function() {16 var callback = sinon.spy();17 test(callback);18 callback.should.have.been.called;19 });20});21var test = function(callback) {22 callback();23};24module.exports = test;25var sinonTest = require('sinon-test');26sinon.test = sinonTest.configureTest(sinon);27var sinonTest = require('sinon-test');28var chai = require('chai');29var sinonChai = require('sinon-chai');30var expect = chai.expect;31chai.use(sinonChai);32var mock = sinon.mock;33var stub = sinon.stub;34var spy = sinon.spy;35var assert = chai.assert;36var should = chai.should;37sinon.test = sinonTest.configureTest(sinon);38var sinonTest = require('sinon-test');39var chai = require('chai');40var sinonChai = require('sinon-chai');41var expect = chai.expect;42chai.use(sinonChai);43var mock = sinon.mock;44var stub = sinon.stub;45var spy = sinon.spy;46var assert = chai.assert;47var should = chai.should;48sinon.test = sinonTest.configureTest(sinon);

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var sinonMock = sinon.mock();3sinonMock.expects('foo').once().withArgs(42).returns('bar');4sinonMock.verify();5var sinon = require('sinon');6var sinonMock = sinon.mock();7sinonMock.expects('foo').once().withArgs(42).returns('bar');8sinonMock.verify();9var sinon = require('sinon');10var sinonMock = sinon.mock();11sinonMock.expects('foo').once().withArgs(42).returns('bar');12sinonMock.verify();13var sinon = require('sinon');14var sinonMock = sinon.mock();15sinonMock.expects('foo').once().withArgs(42).returns('bar');16sinonMock.verify();17var sinon = require('sinon');18var sinonMock = sinon.mock();19sinonMock.expects('foo').once().withArgs(42).returns('bar');20sinonMock.verify();21var sinon = require('sinon');22var sinonMock = sinon.mock();23sinonMock.expects('foo').once().withArgs(42).returns('bar');24sinonMock.verify();25var sinon = require('sinon');26var sinonMock = sinon.mock();27sinonMock.expects('foo').once().withArgs(42).returns('bar');28sinonMock.verify();29var sinon = require('sinon');30var sinonMock = sinon.mock();31sinonMock.expects('foo').once().withArgs(42).returns('bar');

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinonMock = require('sinon-mock')2var sinon = require('sinon')3var request = require('request')4var chai = require('chai')5var chaiAsPromised = require('chai-as-promised')6chai.use(chaiAsPromised)7var sinonMock = sinon.mock(request)8var promise = new Promise(function(resolve, reject) {9 resolve('success')10})11sinonMock.expects('get').returns(promise)12expect(result).to.eventually.equal('success')13sinonMock.verify()14sinonMock.restore()15{16 "dependencies": {17 },18 "devDependencies": {},19 "scripts": {20 },21}

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var sinonMock = sinon.mock();3sinonMock.expects('method').once().withArgs('arg').returns('return value');4sinonMock.method('arg');5sinonMock.verify();6var sinon = require('sinon');7var sinonMock = sinon.mock();8sinonMock.expects('method').once().withArgs('arg').returns('return value');9sinonMock.method('arg');10sinonMock.verify();11var sinon = require('sinon');12var sinonMock = sinon.mock();13sinonMock.expects('method').once().withArgs('arg').returns('return value');14sinonMock.method('arg');15sinonMock.verify();16var sinon = require('sinon');17var sinonMock = sinon.mock();18sinonMock.expects('method').once().withArgs('arg').returns('return value');19sinonMock.method('arg');20sinonMock.verify();21var sinon = require('sinon');22var sinonMock = sinon.mock();23sinonMock.expects('method').once().withArgs('arg').returns('return value');24sinonMock.method('arg');25sinonMock.verify();26var sinon = require('sinon');27var sinonMock = sinon.mock();28sinonMock.expects('method').once().withArgs('arg').returns('return value');29sinonMock.method('arg');30sinonMock.verify();31var sinon = require('sinon');32var sinonMock = sinon.mock();33sinonMock.expects('method').once().withArgs('arg').returns('return value');34sinonMock.method('arg');35sinonMock.verify();36var sinon = require('sinon');37var sinonMock = sinon.mock();38sinonMock.expects('method').once().withArgs('arg').returns('return value');39sinonMock.method('arg');40sinonMock.verify();

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var sinonMock = sinon.mock(obj);3sinonMock.expects('method').once().withArgs('arg').returns('value');4sinonMock.restore();5sinonMock.verify();6var sinon = require('sinon');7var sinonExpectation = sinon.expectation.create('method');8sinonExpectation.once();9sinonExpectation.withArgs('arg');10sinonExpectation.returns('value');11sinonExpectation(obj);12var sinon = require('sinon');13var sinonExpectation = sinon.expectation.create('method');14sinonExpectation.once();15sinonExpectation.withArgs('arg');16sinonExpectation.returns('value');17sinonExpectation(obj);18sinonExpectation.verify();19var sinon = require('sinon');20var sinonExpectation = sinon.expectation.create('method');21sinonExpectation.once();22sinonExpectation.withArgs('arg');23sinonExpectation.returns('value');24sinonExpectation(obj);25sinonExpectation.verify();26sinonExpectation.restore();27var sinon = require('sinon');28var sinonExpectation = sinon.expectation.create('method');29sinonExpectation.once();30sinonExpectation.withArgs('arg');31sinonExpectation.returns('value');32sinonExpectation(obj);33sinonExpectation.verify();34sinonExpectation.restore();35sinonExpectation(obj);36var sinon = require('sinon');37var sinonExpectation = sinon.expectation.create('method');38sinonExpectation.once();39sinonExpectation.withArgs('arg');40sinonExpectation.returns('value');41sinonExpectation(obj);42sinonExpectation.verify();43sinonExpectation.restore();44sinonExpectation(obj);45sinonExpectation.verify();46var sinon = require('sinon');47var sinonExpectation = sinon.expectation.create('method');48sinonExpectation.once();49sinonExpectation.withArgs('arg');50sinonExpectation.returns('

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var sinonMock = require('sinon-mock');3var assert = require('assert');4var mock = sinonMock(sinon);5var obj = {6 method: function() {7 console.log('called method');8 }9};10mock.expects('method').once();11obj.method();12mock.verify();13mock.restore();14assert.ok(mock.verify());

Full Screen

Using AI Code Generation

copy

Full Screen

1const sinon = require('sinon');2const assert = require('assert');3const http = require('http');4const sinonMock = sinon.mock(http);5const request = require('request');6const expectedBody = { success: true };7 assert.equal(body, expectedBody);8 sinonMock.verify();9});10{11 "scripts": {12 },13 "dependencies": {14 }15}

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinonMock = require('sinon').mock;2var mock = sinonMock(object);3mock.expects('method').once().withArgs(42).returns('hello');4var sinonExpectation = require('sinon').expectation;5var expectation = sinonExpectation.create('method');6expectation.once();7expectation.withArgs(42);8expectation.returns('hello');9expectation(object);10var sinonStub = require('sinon').stub;11var stub = sinonStub(object, 'method', function() {12 return 'hello';13});14var sinonSpy = require('sinon').spy;15var spy = sinonSpy(object, 'method');16var sinonStub = require('sinon').stub;17var stub = sinonStub();18stub.withArgs(42).returns('hello');19var sinonSpy = require('sinon').spy;20var spy = sinonSpy();21spy.withArgs(42);22var sinonStub = require('sinon').stub;23var stub = sinonStub();24stub.withArgs(42).returns('hello');25stub.withArgs(24).returns('world');26var sinonSpy = require('sinon').spy;27var spy = sinonSpy();28spy.withArgs(42);29spy.withArgs(24);30var sinonStub = require('sinon').stub;31var stub = sinonStub();32stub.withArgs(42).returns('hello');33stub.withArgs(24).returns('world');34stub.withArgs(12).returns('foo');35var sinonSpy = require('sinon').spy;36var spy = sinonSpy();37spy.withArgs(42);38spy.withArgs(24);39spy.withArgs(12);

Full Screen

Using AI Code Generation

copy

Full Screen

1var assert = require('assert');2var sinon = require('sinon');3var sinonMock = sinon.mock;4var module = require('module');5var moduleMock = sinonMock(module);6var moduleMockObj = moduleMock.expects('method').once().returns('mocked');7var result = module.method();8assert.equal(result, 'mocked');9moduleMock.verify();10moduleMock.restore();11Your name to display (optional):

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinonMock = require('sinon');2var test = require('./test');3describe('test', function() {4 it('should return true', function() {5 var m = sinonMock.mock(test);6 m.expects('test').once().returns(true);7 test.test().should.equal(true);8 m.verify();9 m.restore();10 });11});

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