FLUTTER ECOSYSTEM

stelynx/network_image_mock

Flutter 위젯 테스트를 통과하도록 도와주는 Image.network에 대한 모크 응답입니다.

network_image_mock 프로젝트 이미지
Stars
28
Forks
14
최근 푸시(UTC)
2023. 4. 3.
프로젝트 상태
활성
Stelynx GitHub avatar
GITHUB Organization

Stelynx ↗

언어Dart

기술 주제

이 저장소가 배포한 패키지

사용 중인 의존성

의존성 목록 3 개
  • flutter{"sdk":"flutter"}
  • mockito^5.2.0
  • flutter_test개발 의존성{"sdk":"flutter"}

원본 README

아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.

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.