カテゴリー・パッケージ・リポジトリを検索

パッケージ詳細v2.1.1

network_image_mock

Flutter ウィジェットテストで Image.network の応答をモックするためのユーティリティ。

AndroidiOSmacOSWindowsLinux
30日間ダウンロード
27.2万
いいね数
66
Pub ポイント
160

パッケージをインストール

flutter pub add network_image_mock

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

FlutterウィジェットテストでImage.networkの応答をモックするためのユーティリティパッケージです。実際のHTTPリクエストを行わずにネットワーク画像の読み込みをシミュレートできるため、一貫性があり信頼できるテスト結果が得られます。リモートソースからの画像を表示するUIコンポーネントのテストに最適で、テスト中にプレースホルダーアイテムや事前に定義されたアセットを返す簡単な方法を提供します。このパッケージは軽量であり、Flutterのテストフレームワークとスムーズに統合されます。

用途

  • リモート画像を表示するウィジェットのテスト
  • 画像の読み込み状態をシミュレートする
  • ユニットテストでのネットワーク呼び出しを回避する
  • 画像のプレースホルダーの検証
  • テストの一貫性を確保する

主な機能

  • Image.network の応答をモックする
  • プレースホルダーアイコンをサポート
  • 外部ネットワークの依存関係なし
  • テストファイルとの簡単な統合
  • すべての Flutter プラットフォームで動作します

適している対象

  • Flutter開発者がWidgetテストを書く
  • 安定したテスト環境を必要とするチーム
  • 画像を含む頻繁なUI変更を行うプロジェクト
  • テスト自動化エンジニア
  • CI/CD パイプライン

注意事項

  • 本番環境での使用を想定していません
  • テストファイルで手動設定が必要です
  • 画像モックのシナリオに限定
  • 画像キャッシュを処理しません
  • プロダクション依存関係から削除する必要があります

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

Network Image Mock

Pub Version Lint & Test codecov.io Null safety No null safety GitHub Repo stars GitHub contributors GitHub code size in bytes GitHub

A utility for providing mocked response to Image.network in Flutter widget tests.

Introduction

Since you are here you probably already know that calling Image.network results in 400 response in Flutter widget tests. The reason for this is that default HTTP client in tests always return a 400.

So, what can we do about it? Instead of copying the whole code over and over again for mocking the HTTP client, I created this package. It is heavily inspired by roughike/image_test_utils, however that package is not being maintained despite multiple pull requests asking for bumping the mockito version and making the package usable again.

Installing

Null safety

This package should be installed under dev_dependencies with

dev_dependencies:
  network_image_mock: ^2.0.1
Without null safety (deprecated)

If you have legacy unmigrated code that does not use Null safety, install under dev_dependencies with

dev_dependencies:
  network_image_mock: 1.1.0

Example

The package is quite straightforward to use. All you have to do is include it in your test file and wrap widget testing functions that require proper Image.network response in mockNetworkImagesFor() function provided by this package. A full test example could look like this.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:network_image_mock/network_image_mock.dart';

Widget makeTestableWidget() => MaterialApp(home: Image.network(''));

void main() {
  testWidgets(
    'should properly mock Image.network and not crash',
    (WidgetTester tester) async {
      mockNetworkImagesFor(() => tester.pumpWidget(makeTestableWidget()));
    },
  );
}

This is actually an example taken from tests for this package.

Contributing

There is not much to contribute since the package serves its purpose, however, in chance of needing to bump or adjust some version, or any other suggestion for that matter, please read CONTRIBUTING.

カテゴリー

依存関係

依存関係一覧 1 件

ソースリポジトリ

このパッケージを使用しているプロジェクト