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?

初めてFlutterでModelに対するテストコードを書いてみた

Posted at
  • 構造体の定義も済んだので、せっかくなのでテストコードを実装してみた。

  • ルートクラスTitleには、3つのパラメータ title(String) cover(Coverクラス) content(Contentクラス)が存在する。contentはnullを許容するものとする。

  • Coverクラスには、4つのパラメータ author(String) name(String) createdAt(DateTime) updatedAt(DateTime)が存在する。

完成

model_title_test.dart
import 'package:flutter_test/flutter_test.dart';
import 'package:app/model/titles.dart';

void main() {
  test('Titleクラスのテスト', () {
    final cover = Cover(
      author: '佐藤 太郎', 
      name: '或る夏の憧憬', 
      createdAt: DateTime.now(), 
      updatedAt: DateTime.now()
    );
    final content = Content(chapters: []);

    final titleInstance = Title(
      title: 'アプリタイトル',
      cover: cover,
      content: content,
    );

    print(cover);

    expect(titleInstance.title, 'アプリタイトル');
    expect(titleInstance.cover, cover);
  });
}
  • [Debug]を実行

✅Titleクラスの定義

  • こんな感じでテスト項目に対するテスト結果がOKかNGを教えてくれる。すごい。

  • printを使うことで、coverの中身も調べることが出来る。

Cover(author: 佐藤 太郎, name: 或る夏の憧憬, createdAt: 2025-10-15 11:34:46.878722, updatedAt: 2025-10-15 11:34:46.878722)


  • テストコードを初めて触ってみたが、テスト項目によってはとんでもなく大化けして活躍してくれそう。
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?