njsetn:advanced_testing_sinon_stubs
Stubs → = spies++, en gros permet de contrôler le comportement d'une fonction en particulier.
// order-spec.js (...) this.console = (...); this.warehouse = { packageAndShip: sinon.stub().yelds(0123456789) // la fonction yelds va invoquer le callback refilé lors de l'appelle de packageAndShip //0123456789 = faux tracking number attendu } order.__set__("inventoryData", this.testData); order.__set__("console", this.console); order.__set__("warehouse", this.warehouse); (...) it("order (...), function() { (...) }); describe("Warehouse interaction", function() { beforeEach(function() { this.callback = sinon.spy(); order.orderItem("CCC", 2, this.callback); }); it("receive a tracking number", function() { expect(this.callback.calledWith(0123456789).to.equal(true); }); it("call packageAndShip with the correct sku and quantity", function() { expect(this.warehouse.packageAndShip.calledWith("CCC", 2)).to.equal(true);; }); });
njsetn/advanced_testing_sinon_stubs.txt · Last modified: 2016/03/28 15:28 by leo