mock function nodejs mocha code example
Example: nodejs mocha mock
const chai = require("chai");
const expect = chai.expect;
const sinon = require("sinon");
const indexPage = require("../../controllers/app.controller.js");
describe("AppController", function() {
describe("getIndexPage", function() {
it("should send hey when user is logged in", function() {
let user = {
isLoggedIn: function(){}
}
const isLoggedInStub = sinon.stub(user, "isLoggedIn").returns(true);
let req = {
user: user
}
let res = {
send: function(){}
}
const mock = sinon.mock(res);
mock.expects("send").once().withExactArgs("Hey");
indexPage.getIndexPage(req, res);
expect(isLoggedInStub.calledOnce).to.be.true;
mock.verify();
});
});
});