0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DDDでポケモンバトルを実装する(Step4:ポケモンのバトルのテストを実装する)

0
Posted at

はじめに

前回までの記事の続きです。今回はポケモンバトルのテストコードを実装していきます。

成果物

src/services/Battle.test.ts
import { Pokemon } from "@entities/Pokemon";
import { PokemonType } from "@enums/PokemonType";
import { Stats } from "@entities/Stats";
import { Move } from "@entities/Move";
import { Battle } from "@services/Battle";

describe("Battle", () => {
	let pikachu: Pokemon;
	let bulbasaur: Pokemon;
	let battle: Battle;

	beforeEach(() => {
		const pikachuStats: Stats = { hp: 100, attack: 55, defense: 40, speed: 90 };
		const bulbasaurStats: Stats = {
			hp: 100,
			attack: 49,
			defense: 49,
			speed: 45,
		};

		const thunderbolt: Move = {
			name: "Thunderbolt",
			type: PokemonType.Electric,
			power: 90,
		};
		const tackle: Move = {
			name: "Tackle",
			type: PokemonType.Normal,
			power: 40,
		};

		pikachu = new Pokemon(25, "Pikachu", [PokemonType.Electric], pikachuStats, [
			thunderbolt,
		]);
		bulbasaur = new Pokemon(
			1,
			"Bulbasaur",
			[PokemonType.Grass, PokemonType.Poison],
			bulbasaurStats,
			[tackle],
		);

		battle = new Battle(pikachu, bulbasaur);
	});

	test("バトルを1回実行", () => {
		battle.battleTurn();
		expect(bulbasaur.getStats().hp).toBeLessThanOrEqual(10); // Bulbasaur takes 90 damage
		expect(pikachu.getStats().hp).toBe(60); // Pikachu takes 40 damage
	});

	test("バトルを2回実行し、ピカチュウが勝利", () => {
		battle.battleTurn(); // First turn
		battle.battleTurn(); // Second turn
		expect(pikachu.getStats().hp).toBe(60); // Pikachu takes 40 damage
		expect(bulbasaur.getStats().hp).toBeLessThanOrEqual(0); // Bulbasaur is defeated
	});
});

beforeEachでテストの準備をしています。ここでは対戦する2匹のポケモンを準備しています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?