dart-lang / mockito

Mockito-inspired mock library for Dart
https://pub.dev/packages/mockito
Apache License 2.0
623 stars 160 forks source link

'Null' is not a subtype of type 'Future<bool>' #742

Closed avdhootu27 closed 2 months ago

avdhootu27 commented 2 months ago

I am trying to mock a static method from the class MyDatabase which returns Future. So I created MockMyDatabaseWrapper as:

class MyDatabaseWrapper {
  Future<bool> insertFolder(Folder folder) async => await MyDatabase.insertFolder(folder);
}

class MockMyDatabaseWrapper extends Mock implements MyDatabaseWrapper {}

and in test I am mocking it as:

MockMyDatabaseWrapper mockMyDatabaseWrapper = MockMyDatabaseWrapper();
Folder testFolder = Folder(title: testFolderTitle);
when(await mockMyDatabaseWrapper.insertFolder(testFolder)).thenReturn(true);

but at line when(await mockMyDatabaseWrapper.insertFolder(testFolder)).thenReturn(true); I am getting an error: The following _TypeError was thrown running a test: type 'Null' is not a subtype of type 'Future<bool>'

Can anyone please tell me what I am doing wrong here?

avdhootu27 commented 2 months ago

And one more thing, I am not able to use 'any' as parameter for insertFolder while mocking

srawlins commented 2 months ago

So I created MockMyDatabaseWrapper as:

Definitely don't do that.

Please read in the README how to set up mocks.

avdhootu27 commented 2 months ago

Thanks for this, but how to mock static methods?