我在ES6类中有一个函数,该函数使用“ new”关键字调用另一个类的构造函数。当我使用sinon测试该类时,在测试中出现错误,指出以下内容:“ TypeError:游戏不是构造函数”。这是我需要测试的功能的代码:
createMatch(hostID, pub) {
let matchID = mongoose.Types.ObjectId().toString();
let game = new Game(matchID);
game.init();
game.setHost(hostID);
....
}
这是测试本身。我试过使用createStubInstance(),callsFake等,但没有任何效果:
it("Need to group private and public games into separate maps.", () => {
//Tried to stub the constructor here
const publicMatch = MatchManager.createMatch("aHost1", true);
const privateMatch = MatchManager.createMatch("aHost2", false);
notEqual(publicMatch, undefined);notEqual(publicMatch, null);
notEqual(privateMatch, undefined);notEqual(privateMatch, null);
notEqual(publicMatch.matchID, undefined);
notEqual(privateMatch.matchID, undefined);
notEqual(publicMatch.matchID, null);
notEqual(privateMatch.matchID, null);
notEqual(MatchManager.publicMatches.get("aHost1"), undefined);
equals(MatchManager.publicMatches.get(publicMatch.matchID), true);
equals(MatchManager.privateMatches.get("aHost1"), undefined);
notEqual(MatchManager.privateMatches.get("aHost2"), undefined)
equals(MatchManager.privateMatches.get(privateMatch.matchID), false);
notEqual(MatchManager.publicMatches.get("aHost2"), undefined)
});