Por alguna razón estoy recibiendo:
Pasa la primera prueba: "Amar a todos"
2da prueba falla: Esperado: "Quince amor" Recibido: "Quince amorAmar a todos"
Según tengo entendido, necesito usar afterEach para derribar la configuración original. He intentado esto pero no parece 'borrar' el totalScore.
¡Gracias por tu ayuda!
Archivo de prueba:
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"); }); });Archivo 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;Su prueba está funcionando bien, lo que necesita actualizar es su código de producción.
La lógica actual, cuando firstPlayerScoreTimes es 1, establece el puntaje como Fifteen love y también agrega Love all string al puntaje. Supongo que este no es su requisito. Actualicemos la función getScore :
getScore() { if (this.firstPlayerScoreTimes === 1) { return this.totalScore += "Fifteen love"; // stop, that's enough } this.totalScore += "Love all"; }Elimine += en su método getScore en la clase Tennis , ya que agregará la nueva cadena a la cadena anterior.
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;