felangel / mocktail

A mock library for Dart inspired by mockito
https://pub.dev/packages/mocktail
MIT License
588 stars 80 forks source link

Verify of objects passed in isolates don't work #234

Closed elkSal closed 3 weeks ago

elkSal commented 3 months ago

Isolates in dart copy the objects of the arguments passed as mentioned here.

I have a repository class that has a source and a mapper. I would like to verify that the mapper function is called but the verify doesn't work as the mapper object (passed as an argument of the isolate) is copied inside the isolate, and therefore is not the same object.

Repository code:

class Repository{
final Source source;
final Mapper mapper;
Repository({required this.source, required this.mapper});

Future<List<MappedData>> getMappedData() async {
 final rawData = source.getRawData();
 final mappedData = await Isolate.run(()=>
 _mapRawData(rawData, mapper);
 );
 return mappedData;
 }

 static List<MappedData> _mapRawData(List<RawData> rawData, Mapper mapper){
 return rawData.map((e)=> mapper(e)).toList();
 }

}

Testing code

import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class MockSource extends Mock implements Source{}
class MockMapper extends Mock implements Mapper{}

void main() {
 late MockSource source;
 late MockMapper mapper;
 late Repository repository;

 final rawData = [
 RawData(1),
 RawData(2),
 };

 final mappedData = [
 MappedData(1),
 MappedData(2),
 ];

 setup(){
 source= MockSource();
 mapper = MockMapper();
 repository = Repository (source:source, mapper: mapper);
 }

 test(
      'Repository getMappedData should call source and mapper',
      () async {
  //assign
  when(()=> source.getRawData).thenReturn(rawData);
  when(()=> mapper(rawData[0])).thenReturn(mappedData[0]);
  when(()=> mapper(rawData[1])).thenReturn(mappedData[1]);
  //act
  final result = await source.getMappedData();
  //assert
  expect(result, mappedData);
  verify(()=> source.getRawData).called(1);
  verify(()=> mapper(rawData[0])).called(1);
  verify(()=> mapper(rawData[1])).called(1);
}

The expect works fine, only the verify methods trigger an error as they are never called.

felangel commented 2 months ago

Hi @elkSal đź‘‹ Can you please share a link to a minimal reproduction sample? It would be much easier to help if I can run the sample locally and reproduce the issue, thanks!

felangel commented 3 weeks ago

Closing for now since this issue is quite old and there isn't a minimal reproduction sample. If this is still a problem please file a new issue with a link to a reproduction sample, thanks!