diff --git a/pubspec.yaml b/pubspec.yaml index 01250689..76b9fbfe 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/fake_cache_manager.dart b/test/fake_cache_manager.dart index 0d5505cd..7332e086 100644 --- a/test/fake_cache_manager.dart +++ b/test/fake_cache_manager.dart @@ -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))); } @@ -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, @@ -74,27 +78,31 @@ class FakeCacheManager extends Mock implements CacheManager { class FakeImageCacheManager extends Mock implements ImageCacheManager { ExpectedData returns( - String url, - List imageData, { - Duration delayBetweenChunks, - }) { + String url, + List imageData, { + Duration delayBetweenChunks, + }) { const chunkSize = 8; final chunks = [ 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, @@ -104,11 +112,11 @@ class FakeImageCacheManager extends Mock implements ImageCacheManager { } Stream _createResultStream( - String url, - List chunks, - List imageData, - Duration delayBetweenChunks, - ) async* { + String url, + List chunks, + List imageData, + Duration delayBetweenChunks, + ) async* { var totalSize = imageData.length; var downloaded = 0; for (var chunk in chunks) { diff --git a/test/image_cache_manager_test.dart b/test/image_cache_manager_test.dart index a662ef09..1016cd2e 100644 --- a/test/image_cache_manager_test.dart +++ b/test/image_cache_manager_test.dart @@ -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(); @@ -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 { @@ -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 {