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.

はじめに

前回までの記事の続きです。Entitiyのコードを作成したので、テストコードを書いていきます。
Unitテスト等の細かい単位でテストを実施していくことで、品質だけでなく生産性も向上していきます。

ソースコード

ディレクトリ
├── test
│   └── domain
│       └── entities
│           ├── move_test.dart
│           ├── pokemon_test.dart
│           └── stats_test.dart
test/domain/entities/move_test.dart
import 'package:flutter_test/flutter_test.dart';
import 'package:pokemon_battle_app/domain/entities/move.dart';

void main() {
  group('Move', () {
    test('should create a Move with correct properties', () {
      final move = Move(name: 'Thunderbolt', power: 90);

      expect(move.name, 'Thunderbolt');
      expect(move.power, 90);
    });
  });
}
test/domain/entities/pokemon_test.dart
import 'package:flutter_test/flutter_test.dart';
import 'package:pokemon_battle_app/domain/entities/move.dart';
import 'package:pokemon_battle_app/domain/entities/pokemon.dart';
import 'package:pokemon_battle_app/domain/entities/stats.dart';
import 'package:pokemon_battle_app/domain/enums/pokemon_type.dart';

void main() {
  group('Pokemon', () {
    test('should create a Pokemon with correct properties', () {
      final move1 = Move(name: 'Thunderbolt', power: 90);
      final move2 = Move(name: 'Quick Attack', power: 40);
      final moves = [move1, move2];
      final stats = Stats(hp: 35, attack: 55, defense: 40, speed: 90);
      final pokemon = Pokemon(
        name: 'Pikachu',
        imageUrl:
            'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/25.png',
        moves: moves,
        stats: stats,
        type: PokemonType.electric,
      );

      expect(pokemon.name, 'Pikachu');
      expect(pokemon.imageUrl,
          'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/25.png');
      expect(pokemon.moves, moves);
      expect(pokemon.stats.hp, 35);
      expect(pokemon.stats.attack, 55);
      expect(pokemon.stats.defense, 40);
      expect(pokemon.stats.speed, 90);
      expect(pokemon.type, PokemonType.electric);
    });
  });
}
test/domain/entities/stats_test.dart
import 'package:flutter_test/flutter_test.dart';
import 'package:pokemon_battle_app/domain/entities/stats.dart';

void main() {
  group('Stats', () {
    test('should create Stats with correct properties', () {
      final stats = Stats(hp: 35, attack: 55, defense: 40, speed: 90);

      expect(stats.hp, 35);
      expect(stats.attack, 55);
      expect(stats.defense, 40);
      expect(stats.speed, 90);
    });
  });
}

インスタンスを作成し、期待値と比較するだけです。

実行結果
~/develop/pokemon_battle_app (main)$ flutter test
00:02 +3: All tests passed!  
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?