搜索分类、插件与代码仓

插件详情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 项

源码仓库

使用此插件的项目