Home > Enterprise >  angular typescript write test for random username method
angular typescript write test for random username method

Time:09-25

i started learnin Angular and Typescript and i have to test this method but i dont know how to test Random return . if you can help me would really appreciate

botRandName(): string {
        const x: number = Math.floor(Math.random() * 3);
        if (x === 1) return (this.botName = 'player1');
        else if (x === 2) return (this.botName = 'player2');
        else return (this.botName = 'player3');
    }

CodePudding user response:

I would change your function - to have the ability to pass specific condition:

function botRandName(x = Math.floor(Math.random() * 3)) {
  if (x === 1) return (this.botName = "player1");
  else if (x === 2) return (this.botName = "player2");
  else return (this.botName = "player3");
}
describe("Testing botRandName", () => {
  it("should test", () => {
    expect(botRandName(1)).toBe("player1");
    expect(botRandName(2)).toBe("player2");
    expect(botRandName(3)).toBe("player3");
  });
});

  • Related