在sinon中使用“ new”关键字测试功能

问题描述

我在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)
});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)