【发布时间】:2020-09-09 04:43:20
【问题描述】:
我遵循了中型指南: https://medium.com/@tehvicke/integration-and-unit-testing-with-jest-in-nodejs-and-mongoose-bd41c61c9fbc 正在尝试开发测试套件。我的代码和他的完全一样,但我遇到了 TypeError: Tournament is not a constructor
我放了一些代码,所以你可以看到我在做什么。
TournamentService.js
const createTournament = (Tournament) => (tournamentObj) => {
const {name, creator} = tournamentObj;
const newTournament = new Tournament({name, creator});
return newTournament.save();
};
TournamentService.test.js
const TournamentService = require("../TournamentService");
const sinon = require("sinon");
describe("create Tournament test", () => {
it("creates a tournament", () => {
const save = sinon.spy();
console.log("save ", save);
let name;
let creator;
const MockTournamentModel = (tournamentObject) => {
name = tournamentObject.name;
creator = tournamentObject.creator;
return {
...tournamentObject,
save,
};
};
const tournamentService = TournamentService(MockTournamentModel);
const fintoElemento = {
name: "Test tournament",
creator: "jest",
};
tournamentService.createTournament(fintoElemento);
const expected = true;
const actual = save.calledOnce;
expect(actual).toEqual(expected);
expect(name).toEqual("Test tournament");
});
});
【问题讨论】:
标签: javascript node.js mongoose jestjs sinon