Javascript 使用“测试函数”;新";sinon中的关键字
我在ES6类中有一个函数,它使用“new”关键字调用另一个类的构造函数。当我使用sinon测试该类时,我得到一个错误,在测试中声明如下:“TypeError:Game不是构造函数”。下面是我需要测试的函数的代码:Javascript 使用“测试函数”;新";sinon中的关键字,javascript,constructor,sinon,Javascript,Constructor,Sinon,我在ES6类中有一个函数,它使用“new”关键字调用另一个类的构造函数。当我使用sinon测试该类时,我得到一个错误,在测试中声明如下:“TypeError:Game不是构造函数”。下面是我需要测试的函数的代码: createMatch(hostID, pub) { let matchID = mongoose.Types.ObjectId().toString(); let game = new Game(matchID); game.init(); game.
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)
});