dart-lang / mockito

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

Mocking the (AutoDispose)FamilyNotifier with code generation incorrect #694

Open 5hirish opened 9 months ago

5hirish commented 9 months ago

On trying to mock a Riverpod StateNotifierProvider of (AutoDispose)FamilyNotifier -

@Riverpod(keepAlive: true)
class ProjectDirectoryPath extends _$ProjectDirectoryPath {
  @override
  String? build() {
    return null;
  }
}

Mockito code generation fails to generate the appropriate mock for it -

@GenerateNiceMocks([
  MockSpec<ProjectDirectoryPath>(),
])
void main() {
   container = ProviderContainer(overrides: [
        projectDirectoryPathProvider
            .overrideWith(() => mockProjectDirectoryPath),
      ]);
}

Error -

NoSuchMethodError: Class 'MockProjectDirectoryPath' has no instance method '_setElement'.
Receiver: Instance of 'MockProjectDirectoryPath'
Tried calling: _setElement(Instance of 'NotifierProviderElement<ProjectDirectoryPath, String?>')

How can I generate the appropriate mock using the code generation of mockito? This issue is explored here in depth - https://github.com/rrousselGit/riverpod/issues/2373

andersjoh commented 7 months ago

We are seeing the same issue, will add more information. @5hirish did you find a workaround?