Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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);
    });
});

When I run the above Jasmine test, I get the below error. I think that I might need to mock something, since it looks like there's some sort of recursion. Does anyone have suggestions on how to properly test the constructor?

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)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're seeing a stack overflow because the names of the instance properties and the getters/setters are identical. You need to use a different name for your internal properties to use this approach. The convention is generally to prefix the name with a _ like so...

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

    //...

    get winningPoints() {
        return this._winningPoints
    }

    set winningPoints(winningPoints) {
        this._winningPoints = winningPoints
    }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda