I use Mockito for writing tests on Flutter. I have a mocked class and method with arguments that are functions - Function() and this method returns StreamSubscription
. I need to pass these arguments to the call of listen()
function but can't find a way to do it. (See example)
Could somebody help me, please?
I tried to pass them with argThat(anyNamed('nameOfArgument')
like in the example, but tests trows error - The "anyNamed" argument matcher is used outside of method stubbing (via when
)
class MockPhotoLibraryService extends Mock implements PhotoLibraryService {}
PhotoLibraryService setupMockPhotoLibraryService() {
final photoLibraryService = MockPhotoLibraryService();
when(
photoLibraryService.getPhotosForPeriod(
onData: anyNamed('onData'),
onDone: anyNamed('onDone'),
onError: anyNamed('onError')),
).thenAnswer((_) => Stream<Photo>.fromFuture(
Future<Photo>.delayed(Duration(milliseconds: 50), () => Photo()))
.listen(argThat(anyNamed('onData')), //need to pass argument named onData
onDone: argThat(anyNamed('onDone')), //need to pass argument named onDone
onError: argThat(anyNamed('onError')), //need to pass argument named onError
cancelOnError: true));
return photoLibraryService;
}
I need these arguments functions to be called by the Future for the correct work of my testable widget.