0Pricing
Flutter Mobile Development · レッスン

ユニットテストとモック

Flutter のビジネスロジックに対する高速で独立したユニットテストを作成し、サービスや API に依存するコードをモックでテストする方法を学びます。

「ユニットテストとモック」はCoddyKit上の無料Flutter Mobile Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlutter Mobile Development学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flutter Mobile Developmentコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

The Testing Pyramid

You have seen widget and integration tests. At the base sits the fastest layer: unit tests that verify a single function or class in isolation.

  • Many unit tests (fast)
  • Fewer widget tests
  • Even fewer integration tests (slow)

Anatomy of a Unit Test

A unit test uses test() and expect() from the test package.

import 'package:test/test.dart';

void main() {
  test('adds two numbers', () {
    expect(2 + 3, equals(5));
  });
}

Testing a Class

Instantiate the class under test and assert on its behaviour.

test('counter increments', () {
  final counter = Counter();
  counter.increment();
  expect(counter.value, 1);
});

Matchers

Matchers express expectations clearly.

  • equals(x)
  • isTrue / isNull
  • throwsException
  • contains(x)
expect(list, contains('apple'));
expect(() => parse('bad'), throwsFormatException);

Grouping Tests

Use group() to organize related tests and share setup.

group('Cart', () {
  test('starts empty', () { ... });
  test('adds items', () { ... });
});

setUp & tearDown

setUp runs before each test; tearDown runs after — perfect for fresh fixtures.

late Cart cart;
setUp(() => cart = Cart());
tearDown(() => cart.clear());

The Problem of Dependencies

What if your class calls a real API or database? Tests would be slow and flaky. The solution is mocking — replacing dependencies with fakes you control.

Mockito Setup

Add mockito and build_runner, then annotate to generate mocks.

@GenerateMocks([ApiService])
void main() {}
// flutter pub run build_runner build

Stubbing Methods

Use when(...).thenReturn(...) to define what a mock returns.

final mockApi = MockApiService();
when(mockApi.fetchUser()).thenAnswer((_) async => User('Ada'));

Verifying Interactions

verify confirms a method was called the expected number of times.

await repository.loadUser();
verify(mockApi.fetchUser()).called(1);

Testing Async Code

Mark the test body async and await futures, or use the completes matcher.

test('loads data', () async {
  final result = await repo.load();
  expect(result, isNotEmpty);
});

Quick Check

Why would you use a mock in a unit test?

Recap

You learned unit testing and mocking:

  • test/expect/matchers and group/setUp
  • Mockito to generate mocks
  • when().thenReturn and verify().called
  • Testing async code

Unit tests give you fast confidence in your business logic.

よくある質問

「ユニットテストとモック」レッスンは無料ですか?

はい。「ユニットテストとモック」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flutter Mobile Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flutter Mobile Developmentコースには全4レッスンが含まれています。

「ユニットテストとモック」で何を学びますか?

Flutter のビジネスロジックに対する高速で独立したユニットテストを作成し、サービスや API に依存するコードをモックでテストする方法を学びます。 ブラウザで直接実行するハンズオンコードでFlutter Mobile Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flutter Mobile Developmentを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlutter Mobile Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「ユニットテストとモック」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlutter Mobile Developmentレッスンでコードを書いて実行できますか?

はい。すべてのFlutter Mobile Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ウィジェットテストの原則
  2. インテグレーションテスト
  3. デバッグツールとテクニック
  4. ユニットテストとモック
← Flutter Mobile Developmentに戻る