GithubHelp home page GithubHelp logo

frederickcxa / image_test_utils Goto Github PK

View Code? Open in Web Editor NEW

This project forked from roughike/image_test_utils

0.0 1.0 0.0 30 KB

Provide mocked responses for Image.network widgets in Flutter widget tests.

License: BSD 2-Clause "Simplified" License

Java 10.66% Objective-C 9.80% Dart 79.55%

image_test_utils's Introduction

image_test_utils

pub package Build Status

Without providing mocked responses, any widget test that pumps up Image.network widgets will crash.

There's a blog post that goes more into detail on this.

Copy-pasting the code for mocking the image responses to every new project gets a little boring. This helper library makes it easier to provide those mocked image responses.

Usage

First, depend on the library:

pubspec.yaml

dev_dependencies:
  image_test_utils: ^1.0.0

Note that this library should be included in your dev_dependencies block; not in your regular dependencies.

In your widget tests, import the library and wrap your widget test in a provideMockedNetworkImages method.

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

void main() {
  testWidgets('should not crash', (WidgetTester tester) async {
    provideMockedNetworkImages(() async {
      /// Now we can pump NetworkImages without crashing our tests. Yay!
      await tester.pumpWidget(
        MaterialApp(
          home: Image.network('https://example.com/image.png'),
        ),
      );
      
      /// Other test code goes here.
    });
  });
}

All HTTP GET requests inside the closure of provideMockedNetworkImages will receive a mocked image response, and your tests will not crash with 404's anymore.

image_test_utils's People

Contributors

roughike avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.