Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
elkSal opened this issue Apr 5, 2024 · 2 comments
Closed

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

elkSal opened this issue Apr 5, 2024 · 2 comments
Labels
question Further information is requested

Comments

@elkSal
Copy link

elkSal commented Apr 5, 2024

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
Copy link
Owner

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 felangel added question Further information is requested waiting for response Waiting for follow up labels Apr 20, 2024
@felangel
Copy link
Owner

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!

@felangel felangel removed the waiting for response Waiting for follow up label Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants