Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用“测试函数”;新";sinon中的关键字_Javascript_Constructor_Sinon - Fatal编程技术网

Javascript 使用“测试函数”;新";sinon中的关键字

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.

我在ES6类中有一个函数,它使用“new”关键字调用另一个类的构造函数。当我使用sinon测试该类时,我得到一个错误,在测试中声明如下:“TypeError: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)
});