乒乓游戲.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;
}
}
乒乓游戲.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)
uj5u.com熱心網友回復:
您會看到堆疊溢位,因為實體屬性和 getter/setter 的名稱是相同的。您需要為內部屬性使用不同的名稱才能使用此方法。約定通常是在名稱前加上一個_像這樣的前綴......
class PingPongGame {
constructor(winningPoints) {
//...
this._winningPoints = winningPoints;
}
//...
get winningPoints() {
return this._winningPoints
}
set winningPoints(winningPoints) {
this._winningPoints = winningPoints
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/391800.html
標籤:javascript 单元测试 测试 茉莉花
上一篇:當所有列回傳的總和為0時,如何在表中的所有列中顯示0?
下一篇:方法內部的角度測驗分支
