createServer.js
const express = require("express");
const mongoConnector = require("./mongoConnector");
const sequelizeConnector = require("./sequelizeConnector");
const {
logger: { logger }
} = require("custom-error-exceptions");
class CreateServer {
constructor(param = {}) {
this.port = param.port || 3000;
this.config = param.config || null;
this.models = param.models;
this.app = express();
this.server;
}
choosingDatabase() {
if (this.config && this.config.dbConfig) {
const {
dbConfig: { dialect },
} = this.config;
if (databases.sequelizeSupportDialect.includes(dialect)) {
logger.info(`Using sequelize dialect ${dialect} database`);
sequelizeConnector(this.app, this.config, logger, this.models);
} else if (dialect === databases.mongodbDialect) {
logger.info("Using mongodb database");
const createMiddleware = mongoConnector(
this.app,
this.config,
logger,
this.models
);
this.app.use(createMiddleware);
} else {
logger.error("Dialect you're choosing ins'nt supported");
}
} else {
logger.info("No database config found");
}
}
}
How to Mocking returned result function call sequelizeConnector & mongoConnector?
I've finished a few lines of code on the choosingDatabase function, but when I do the same on the if (databases.sequelizeSupportDialect.includes(dialect) and else if (dialect === databases.mongodbDialect) blocks it actually calls real connection.how to mock the two functions? i hope someone can help me.
const chai = require("chai");
const sinon = require("sinon");
const sinonChai = require("sinon-chai");
const CreateServer = require("./createServer");
const { expect } = chai;
chai.should();
chai.use(sinonChai);
describe("CreateServer", () => {
describe("choosingDatabase", () => {
it("should return a function that calls createRouter", () => {
const server = new CreateServer({});
sinon.spy(server, "choosingDatabase");
server.choosingDatabase();
expect(server.choosingDatabase).calledOnce;
});
it("should return a function that calls createRouter", () => {
const server = new CreateServer({ config: {} });
sinon.spy(server, "choosingDatabase");
server.choosingDatabase();
expect(server.choosingDatabase).calledOnce;
});
it("should return a function that calls createRouter", () => {
const server = new CreateServer({
config: { dbConfig: { dialect: "wrong" } },
});
sinon.spy(server, "choosingDatabase");
server.choosingDatabase();
expect(server.choosingDatabase).calledOnce;
});
});
})
How to Mocking returned result function call sequelizeConnector & mongoConnector?
I've finished a few lines of code on the choosingDatabase function, but when I do the same on the if (databases.sequelizeSupportDialect.includes(dialect) and else if (dialect === databases.mongodbDialect) blocks it actually calls real connection.how to mock the two functions? i hope someone can help me.