Skip to content

Commit

Permalink
Merge pull request #554 from daohoangson/refactor/mocktail
Browse files Browse the repository at this point in the history
Switch to use mocktail
  • Loading branch information
renefloor authored Mar 2, 2021
2 parents d4b09da + 4f72e43 commit fe2e893
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 54 deletions.
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
mockito: ^4.1.1
mocktail: ^0.0.1-0
pedantic: ^1.9.0
file: ^5.2.1

Expand Down
74 changes: 41 additions & 33 deletions test/fake_cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import 'dart:typed_data';

import 'package:file/memory.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:mockito/mockito.dart';
import 'package:mocktail/mocktail.dart';
import 'dart:async';

class FakeCacheManager extends Mock implements CacheManager {
void throwsNotFound(String url) {
when(getFileStream(
url,
withProgress: anyNamed('withProgress'),
headers: anyNamed('headers'),
key: anyNamed('key'),
)).thenThrow(HttpExceptionWithStatus(404, 'Invalid statusCode: 404',
when(this).calls(#getFileStream).withArgs(
positional: [url],
named: {
#key: any,
#headers: any,
#withProgress: any,
},
).thenThrow(HttpExceptionWithStatus(404, 'Invalid statusCode: 404',
uri: Uri.parse(url)));
}

Expand All @@ -31,12 +33,14 @@ class FakeCacheManager extends Mock implements CacheManager {
Uint8List.fromList(imageData.skip(offset).take(chunkSize).toList()),
];

when(getFileStream(
url,
withProgress: anyNamed('withProgress'),
headers: anyNamed('headers'),
key: anyNamed('key'),
)).thenAnswer((realInvocation) => _createResultStream(
when(this).calls(#getFileStream).withArgs(
positional: [url],
named: {
#key: any,
#headers: any,
#withProgress: any,
},
).thenAnswer((_) => _createResultStream(
url,
chunks,
imageData,
Expand Down Expand Up @@ -74,27 +78,31 @@ class FakeCacheManager extends Mock implements CacheManager {

class FakeImageCacheManager extends Mock implements ImageCacheManager {
ExpectedData returns(
String url,
List<int> imageData, {
Duration delayBetweenChunks,
}) {
String url,
List<int> imageData, {
Duration delayBetweenChunks,
}) {
const chunkSize = 8;
final chunks = <Uint8List>[
for (int offset = 0; offset < imageData.length; offset += chunkSize)
Uint8List.fromList(imageData.skip(offset).take(chunkSize).toList()),
];

when(getImageFile(
url,
withProgress: anyNamed('withProgress'),
headers: anyNamed('headers'),
key: anyNamed('key'),
)).thenAnswer((realInvocation) => _createResultStream(
url,
chunks,
imageData,
delayBetweenChunks,
));
when(this).calls(#getImageFile).withArgs(
positional: [url],
named: {
#key: any,
#headers: any,
#withProgress: any,
#maxHeight: any,
#maxWidth: any,
},
).thenAnswer((_) => _createResultStream(
url,
chunks,
imageData,
delayBetweenChunks,
));

return ExpectedData(
chunks: chunks.length,
Expand All @@ -104,11 +112,11 @@ class FakeImageCacheManager extends Mock implements ImageCacheManager {
}

Stream<FileResponse> _createResultStream(
String url,
List<Uint8List> chunks,
List<int> imageData,
Duration delayBetweenChunks,
) async* {
String url,
List<Uint8List> chunks,
List<int> imageData,
Duration delayBetweenChunks,
) async* {
var totalSize = imageData.length;
var downloaded = 0;
for (var chunk in chunks) {
Expand Down
48 changes: 28 additions & 20 deletions test/image_cache_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

import 'fake_cache_manager.dart';
import 'image_data.dart';
import 'rendering_tester.dart';
import 'package:mockito/mockito.dart';

void main() {
TestRenderingFlutterBinding();
Expand Down Expand Up @@ -45,19 +45,25 @@ void main() {
));
await imageAvailable.future;

verify(cacheManager.getImageFile(
url,
withProgress: anyNamed('withProgress'),
headers: anyNamed('headers'),
key: anyNamed('key'),
)).called(1);

verifyNever(cacheManager.getFileStream(
url,
withProgress: anyNamed('withProgress'),
headers: anyNamed('headers'),
key: anyNamed('key'),
));
verify(cacheManager).called(#getImageFile).withArgs(
positional: [url],
named: {
#key: any,
#headers: any,
#withProgress: any,
#maxHeight: any,
#maxWidth: any,
},
).times(1);

verify(cacheManager).called(#getFileStream).withArgs(
positional: [url],
named: {
#key: any,
#headers: any,
#withProgress: any,
},
).never();
}, skip: isBrowser);

test('Supplying an CacheManager should call getFileStream', () async {
Expand All @@ -78,12 +84,14 @@ void main() {
));
await imageAvailable.future;

verify(cacheManager.getFileStream(
url,
withProgress: anyNamed('withProgress'),
headers: anyNamed('headers'),
key: anyNamed('key'),
)).called(1);
verify(cacheManager).called(#getFileStream).withArgs(
positional: [url],
named: {
#key: any,
#headers: any,
#withProgress: any,
},
).times(1);
}, skip: isBrowser);

test('Supplying an CacheManager with maxHeight throws assertion', () async {
Expand Down

0 comments on commit fe2e893

Please sign in to comment.