network_image_mock 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 80

Network Image Mock #

Pub Version Lint & Test codecov.io

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 #

This package should be installed under dev_dependencies like

dev_dependencies:
  network_image_mock: ^1.0.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.

Changelog #

1.0.1 #

Add example.

1.0.0 #

Official release.

example/main.dart

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Image.network('https://example.com/some_image.jpg'),
    );
  }
}

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

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  network_image_mock: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:network_image_mock/network_image_mock.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
60
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
80
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • network_image_mock that is a package requiring null.

Package not compatible with runtime flutter-web on web

Because:

  • package:network_image_mock/network_image_mock.dart that imports:
  • package:network_image_mock/src/network_image_mock.dart that imports:
  • dart:io

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
mockito ^4.1.1 4.1.1
Transitive dependencies
async 2.4.2
boolean_selector 2.0.0
charcode 1.1.3
collection 1.14.12 1.14.13
matcher 0.12.8
meta 1.1.8 1.2.2
path 1.7.0
sky_engine 0.0.99
source_span 1.7.0
stack_trace 1.9.5
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
test_api 0.2.17
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test