我如何在 Jasmine 中测试构造函数?

How do I test a constructor in Jasmine?

pingPongGame.js

class PingPongGame {
    constructor(winningPoints) {
        this.playerOneScore = 0;
        this.playerTwoScore = 0;
        this.winningPoints = winningPoints;
    }

    get playerOneScore() {
        return this.playerOneScore;
    }

    set playerOneScore(newPlayerOneScore) {
        this.playerOneScore = newPlayerOneScore;
    }

    get playerTwoScore() {
        return this.playerTwoScore;
    }

    set playerTwoScore(newPlayerTwoScore) {
        this.playerTwoScore = newPlayerTwoScore;
    }

    get winningPoints() {
        return this.winningPoints;
    }

    set winningPoints(newWinningPoints) {
        this.winningPoints = newWinningPoints;
    }
}

pingPongGame.spec.js

describe("when the pingPongGame constructor is called", function () {
    it("should create a ping pong game with given arguments.", function () {
        let newPingPongGame = new PingPongGame(10);
        expect(newPingPongGame.getWinningPoints).toBe(10);
    });
});

当我 运行 上面的 Jasmine 测试时,我得到以下错误。我认为我可能需要模拟一些东西,因为它看起来像是某种递归。有人对如何正确测试构造函数有建议吗?

when the pingPongGame constructor is called > should create a ping pong game with given arguments.
RangeError: Maximum call stack size exceeded
RangeError: Maximum call stack size exceeded
    at PingPongGame.set playerOneScore [as playerOneScore] (file:///C:/Users/charl/Documents/WebDev/PingPongScorekeeper/src/pingPongGame.js:13:29)
    at PingPongGame.set playerOneScore [as playerOneScore] (file:///C:/Users/charl/Documents/WebDev/PingPongScorekeeper/src/pingPongGame.js:13:29)
    at PingPongGame.set playerOneScore [as playerOneScore] (file:///C:/Users/charl/Documents/WebDev/PingPongScorekeeper/src/pingPongGame.js:13:29)
    at PingPongGame.set playerOneScore [as playerOneScore] (file:///C:/Users/charl/Documents/WebDev/PingPongScorekeeper/src/pingPongGame.js:13:29)
    at PingPongGame.set playerOneScore [as playerOneScore] (file:///C:/Users/charl/Documents/WebDev/PingPongScorekeeper/src/pingPongGame.js:13:29)
    at PingPongGame.set playerOneScore [as playerOneScore] (file:///C:/Users/charl/Documents/WebDev/PingPongScorekeeper/src/pingPongGame.js:13:29)
    at PingPongGame.set playerOneScore [as playerOneScore] (file:///C:/Users/charl/Documents/WebDev/PingPongScorekeeper/src/pingPongGame.js:13:29)
    at PingPongGame.set playerOneScore [as playerOneScore] (file:///C:/Users/charl/Documents/WebDev/PingPongScorekeeper/src/pingPongGame.js:13:29)
    at PingPongGame.set playerOneScore [as playerOneScore] (file:///C:/Users/charl/Documents/WebDev/PingPongScorekeeper/src/pingPongGame.js:13:29)
    at PingPongGame.set playerOneScore [as playerOneScore] (file:///C:/Users/charl/Documents/WebDev/PingPongScorekeeper/src/pingPongGame.js:13:29)

您看到堆栈溢出是因为实例属性的名称和 getters/setters 相同。您需要为内部属性使用不同的名称才能使用此方法。惯例通常是在名称前加上 _ 像这样...

class PingPongGame {
    constructor(winningPoints) {
        //...
        this._winningPoints = winningPoints;
    }

    //...

    get winningPoints() {
        return this._winningPoints
    }

    set winningPoints(winningPoints) {
        this._winningPoints = winningPoints
    }
}