出于某種原因,我得到:
第一次測驗通過:“愛所有人”
第二次測驗失敗:預期:“十五愛”收到:“十五愛全愛”
據我了解,我需要使用 afterEach 來拆除原始配置。我試過這個,但它似乎沒有“擦除”totalScore。
謝謝你的幫助!
測驗檔案:
const Tennis = require("../src/index");
describe("Tennis game", () => {
let tennis;
beforeEach(() => {
tennis = new Tennis();
});
afterEach(() => {
tennis.resetGame();
});
const scoreShouldBe = (expected) => {
tennis.getScore();
expect(tennis.totalScore).toBe(expected);
};
test("Love all", () => {
scoreShouldBe("Love all");
});
test("Fifteen love", () => {
tennis.firstPlayerScore();
scoreShouldBe("Fifteen love");
});
});
Index.js 檔案:
class Tennis {
constructor() {
this.firstPlayerScoreTimes = 0;
this.totalScore = "";
}
getScore() {
if (this.firstPlayerScoreTimes === 1) {
this.totalScore = "Fifteen love";
}
this.totalScore = "Love all";
}
firstPlayerScore() {
this.firstPlayerScoreTimes ;
}
resetGame() {
this.totalScore = "";
}
}
module.exports = Tennis;
uj5u.com熱心網友回復:
=在類中的getScore方法中洗掉,Tennis因為它將新字串添加到舊字串中。
class Tennis {
constructor() {
this.firstPlayerScoreTimes = 0;
this.totalScore = "";
}
getScore() {
if (this.firstPlayerScoreTimes === 1) {
this.totalScore = "Fifteen love";
}
this.totalScore = "Love all";
}
firstPlayerScore() {
this.firstPlayerScoreTimes ;
}
resetGame() {
this.totalScore = "";
}
}
module.exports = Tennis;
uj5u.com熱心網友回復:
您的測驗運行良好,您需要更新的是您的生產代碼。
當前邏輯,當firstPlayerScoreTimes為 1 時,您將分數設定為Fifteen love并將Love all字串附加到分數。我想這不是你的要求。讓我們更新getScore函式:
getScore() {
if (this.firstPlayerScoreTimes === 1) {
return this.totalScore = "Fifteen love"; // stop, that's enough
}
this.totalScore = "Love all";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/354906.html
標籤:javascript 玩笑
