搜尋分類、套件與程式碼儲存庫

套件詳情v2.1.1

network_image_mock

用於在 Flutter widget 測試中提供模擬的 Image.network 回應的工具。

AndroidiOSmacOSWindowsLinux
近 30 天下載
27.2萬
喜歡數
66
Pub 分數
160

安裝套件

flutter pub add network_image_mock

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個用於在Flutter widget測試中模擬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 項

原始碼儲存庫

使用此套件的專案