Skip to content

Commit

Permalink
test(ThreeSixNineGame): Math.random 메서드 mocking 및 테스트 케이스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Jong1co committed Apr 27, 2024
1 parent a7350e2 commit 086ae63
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 56 deletions.
5 changes: 5 additions & 0 deletions src/Util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class Util {
static random() {
return Math.random();
}
}
110 changes: 54 additions & 56 deletions src/__test__/ThreeSixNineGame.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ThreeSixNineGame, ThreeSixNineGameImpl } from "..";
import { BusanRule } from "../BusanRule";
import { Player, PlayerImpl } from "../Player";
import { SeoulRule } from "../SeoulRule";
import { ThreeSixNineGame, ThreeSixNineGameImpl } from "../TreeSixNineGame";
import { Util } from "../Util";

export const getLogSpy = () => {
const logSpy = jest.spyOn(console, "log");
Expand All @@ -9,71 +11,45 @@ export const getLogSpy = () => {
};

describe("369 게임", () => {
describe("do369", () => {
describe("서울 - do369", () => {
let threeSixNineGame: ThreeSixNineGame;

beforeEach(() => {
threeSixNineGame = new ThreeSixNineGameImpl();
threeSixNineGame = new ThreeSixNineGameImpl(new SeoulRule());
});

it("3이 포함되어 있다면, 박수를 한 번 쳐야 한다.", () => {
it("3, 6, 9가 포함되어 있다면, 박수를 한 번 쳐야 한다.", () => {
expect(threeSixNineGame.do369(3)).toBe("clap");
expect(threeSixNineGame.do369(16)).toBe("clap");
expect(threeSixNineGame.do369(10)).toBe("10"); // 0일 경우에도 나머지 0이 나옴
expect(threeSixNineGame.do369(33)).toBe("clap");
});

it("3이 포함되어 있지 않다면, 숫자를 반환해야 한다.", () => {
it("3, 6, 9가 포함되어 있지 않다면, 숫자를 반환해야 한다.", () => {
expect(threeSixNineGame.do369(4)).toBe("4");
expect(threeSixNineGame.do369(12)).toBe("12");
expect(threeSixNineGame.do369(51)).toBe("51");
});
});

describe("playGame>오답률이 0퍼센트 일 경우", () => {
describe("부산 - do369", () => {
let threeSixNineGame: ThreeSixNineGame;
let players: Player[];

beforeEach(() => {
threeSixNineGame = new ThreeSixNineGameImpl();
players = [
{ name: "짱구", incorrectAnswerRate: 0 }, //
{ name: "훈이", incorrectAnswerRate: 0 },
{ name: "맹구", incorrectAnswerRate: 0 },
{ name: "유리", incorrectAnswerRate: 0 },
].map((variable) => new PlayerImpl(variable));
threeSixNineGame = new ThreeSixNineGameImpl(new BusanRule());
});

it("player의 이름이 포함되어야 한다.", () => {
const logSpy = getLogSpy();
threeSixNineGame.playGame(players);

const messages = Array.from(
{ length: 100 },
(_, i) => players[i % players.length].name
);

// then
messages.forEach((output) => {
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining(output));
});
it("3, 6, 9가 포함되어 있다면, clap", () => {
expect(threeSixNineGame.do369(3)).toBe("clap");
expect(threeSixNineGame.do369(16)).toBe("clap");
expect(threeSixNineGame.do369(10)).toBe("10"); // 0일 경우에도 나머지 0이 나옴
expect(threeSixNineGame.do369(33)).toBe("clapclap");
});

it("정답에 3, 6, 9가 존재해선 안 된다.", () => {
const logSpy = getLogSpy();
threeSixNineGame.playGame(players);

const messages = Array.from(
{ length: 100 },
(_, i) => players[i % players.length]
);

// then
messages.forEach((output) => {
expect(logSpy).not.toHaveBeenCalledWith(expect.stringContaining("3"));
expect(logSpy).not.toHaveBeenCalledWith(expect.stringContaining("6"));
expect(logSpy).not.toHaveBeenCalledWith(expect.stringContaining("9"));
});
it("3, 6, 9가 포함되어 있지 않다면, 숫자를 반환해야 한다.", () => {
expect(threeSixNineGame.do369(4)).toBe("4");
expect(threeSixNineGame.do369(12)).toBe("12");
expect(threeSixNineGame.do369(51)).toBe("51");
});
});

Expand All @@ -82,7 +58,7 @@ describe("369 게임", () => {
let players: Player[];

beforeEach(() => {
threeSixNineGame = new ThreeSixNineGameImpl();
threeSixNineGame = new ThreeSixNineGameImpl(new BusanRule());
players = [
{ name: "짱구", incorrectAnswerRate: 1 }, //
{ name: "훈이", incorrectAnswerRate: 0 },
Expand All @@ -91,9 +67,9 @@ describe("369 게임", () => {
].map((variable) => new PlayerImpl(variable));
});

it("게임을 종료합니다 메세지 ", () => {
it("게임을 종료합니다 메세지 ", async () => {
const logSpy = getLogSpy();
threeSixNineGame.playGame(players);
await threeSixNineGame.playGame(players);

// then
["짱구", "게임을 종료합니다."].forEach((output) => {
Expand All @@ -105,18 +81,40 @@ describe("369 게임", () => {
});
});

describe("부산 369 게임", () => {
describe("do369", () => {
let threeSixNineGame: ThreeSixNineGame;
describe("369게임 > isTurnOfIncorrectAnswer", () => {
let threeSixNineGame: ThreeSixNineGame;

beforeEach(() => {
threeSixNineGame = new BusanRule();
});
beforeEach(() => {
threeSixNineGame = new ThreeSixNineGameImpl(new SeoulRule());
});

it("3의 배수가 포함되어 있다면, 갯수만큼 박수를 쳐야 한다.", () => {
expect(threeSixNineGame.do369(3)).toBe("clap");
expect(threeSixNineGame.do369(393)).toBe("clapclapclap");
expect(threeSixNineGame.do369(33)).toBe("clapclap");
});
it("오답률이 랜덤 숫자보다 작을 경우 정답이어야 한다. (false를 반환해야 한다.)", () => {
Util.random = jest.fn().mockReturnValueOnce(1); // 랜덤 숫자

const result = threeSixNineGame.isTurnOfIncorrectAnswer(
new PlayerImpl({ name: "짱구", incorrectAnswerRate: 0.3 }) //
);

expect(result).toBe(false);
});

it("오답률이 랜덤 숫자보다 클 경우 오답이어야 한다. (true를 반환해야 한다.)", () => {
Util.random = jest.fn().mockReturnValueOnce(0.2); // 랜덤 숫자

const result = threeSixNineGame.isTurnOfIncorrectAnswer(
new PlayerImpl({ name: "짱구", incorrectAnswerRate: 0.3 }) //
);

expect(result).toBe(true);
});

it("오답률이 랜덤 숫자와 같을 경우 오답이어야 한다. (true를 반환해야 한다.)", () => {
Util.random = jest.fn().mockReturnValueOnce(0.3); // 랜덤 숫자

const result = threeSixNineGame.isTurnOfIncorrectAnswer(
new PlayerImpl({ name: "짱구", incorrectAnswerRate: 0.3 }) //
);

expect(result).toBe(true);
});
});

0 comments on commit 086ae63

Please sign in to comment.