https://github.com/roughike/image_test_utils
Provide mocked responses for Image.network widgets in Flutter widget tests.
https://github.com/roughike/image_test_utils
Last synced: 9 months ago
JSON representation
Provide mocked responses for Image.network widgets in Flutter widget tests.
- Host: GitHub
- URL: https://github.com/roughike/image_test_utils
- Owner: roughike
- License: bsd-2-clause
- Created: 2018-09-14T21:27:35.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-07-07T09:05:02.000Z (over 4 years ago)
- Last Synced: 2025-04-14T16:21:53.395Z (9 months ago)
- Language: Dart
- Homepage:
- Size: 29.3 KB
- Stars: 41
- Watchers: 3
- Forks: 25
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# image_test_utils
[](https://pub.dartlang.org/packages/image_test_utils)
[](https://travis-ci.org/roughike/image_test_utils)
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.](https://iirokrankka.com/2018/09/16/image-network-widget-tests/)
Copy-pasting [the code for mocking the image responses](https://github.com/flutter/flutter/blob/master/dev/manual_tests/test/mock_image_http.dart) 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**
```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.
```dart
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.