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?

はじめに

前回の続きです。テストコードを実装していきます。
テストは開発の基本なので、ここら辺は手厚く実施していきたいです。
特にロジック周りは手厚めにやると、経験上吉です。

ソースコード

test/main_test.dart
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:my_first_app/main.dart'; // アプリのエントリーポイントをインポート

void main() {
  testWidgets('じゃんけんゲームのテスト', (WidgetTester tester) async {
    // アプリをビルドしてテスト環境にアタッチ
    await tester.pumpWidget(MyApp());

    // グーのボタンを見つける
    final guButtonFinder = find.text('グー');
    final chokiButtonFinder = find.text('チョキ');
    final paButtonFinder = find.text('パー');

    // グーのボタンが存在するか確認
    expect(guButtonFinder, findsOneWidget);
    expect(chokiButtonFinder, findsOneWidget);
    expect(paButtonFinder, findsOneWidget);

    // グーのボタンをタップ
    await tester.tap(guButtonFinder);
    await tester.pump(); // ステートの変更を反映

    // コンピュータの手と結果が表示されるか確認
    expect(find.byType(Text), findsWidgets); // いくつかのTextウィジェットが存在するはず

    // コンピュータの手と結果を確認
    final computerChoiceFinder = find.textContaining('コンピュータの手:');
    final resultFinder = find.textContaining('結果:');
    expect(computerChoiceFinder, findsOneWidget);
    expect(resultFinder, findsOneWidget);

    // 他のボタンでも同様のテストを行う
    await tester.tap(chokiButtonFinder);
    await tester.pump();
    expect(computerChoiceFinder, findsOneWidget);
    expect(resultFinder, findsOneWidget);

    await tester.tap(paButtonFinder);
    await tester.pump();
    expect(computerChoiceFinder, findsOneWidget);
    expect(resultFinder, findsOneWidget);
  });
}
test/janken_game_test.dart
import 'package:flutter_test/flutter_test.dart';
import 'package:my_first_app/game_logic.dart'; // パッケージ名を修正

void main() {
  group('JankenGame', () {
    JankenGame? game;

    setUp(() {
      game = JankenGame();
    });

    test('初期状態の確認', () {
      expect(game?.userChoice, '');
      expect(game?.computerChoice, '');
      expect(game?.result, '');
    });

    test('ユーザーがグーを選択した場合', () {
      game?.playGame('グー');

      expect(game?.userChoice, 'グー');
      expect(game?.choices, contains(game?.computerChoice));
      expect(['勝ち', '負け', '引き分け'], contains(game?.result));
    });

    test('ユーザーがチョキを選択した場合', () {
      game?.playGame('チョキ');

      expect(game?.userChoice, 'チョキ');
      expect(game?.choices, contains(game?.computerChoice));
      expect(['勝ち', '負け', '引き分け'], contains(game?.result));
    });

    test('ユーザーがパーを選択した場合', () {
      game?.playGame('パー');

      expect(game?.userChoice, 'パー');
      expect(game?.choices, contains(game?.computerChoice));
      expect(['勝ち', '負け', '引き分け'], contains(game?.result));
    });
  });
}
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?