dart-lang / mockito

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

How to parse any() to not null parameter. #687

Closed Nikzed closed 10 months ago

Nikzed commented 10 months ago

I have a use case with not Null incoming parameter.

@override
  Future<bool> call({required DrawerItem params}) async {
  ...
  }

and I do want to test it the next way:

when(_doSomethingUseCase.call(params: any())).thenAnswer((_) => Future.value(false));

but I do have the next problem with any()

An expression whose value is always 'null' can't be dereferenced.
Try changing the type of the expression.dart[invalid_use_of_null_value](https://dart.dev/diagnostics/invalid_use_of_null_value)

Is there any way to mock this behavior?

Nikzed commented 10 months ago

Is this a right way to declare mock classes?

late final MockDoSomethingUseCase _doSomethingUseCase;

setUp(() {
      _doSomethingUseCase= MockDoSomethingUseCase();
    });

Found in readme about stubbing

The when, thenReturn, thenAnswer, and thenThrow APIs provide a stubbing mechanism to override this behavior. Once stubbed, the method will always return stubbed value regardless of how many times it is called. If a method invocation matches multiple stubs, the one which was declared last will be used. It is worth noting that stubbing and verifying only works on methods of a mocked class; in this case, an instance of MockCat must be used, not an instance of Cat.

Thanks to this kind guy issue https://github.com/dart-lang/mockito/issues/364

srawlins commented 10 months ago

but I do have the next problem with any()

Are you certain you are using this mockito package? We do not have a function called any(). There is a top-level property called any.

yanok commented 10 months ago

Anyway, it looks like that's an instance of "you must declare your mocks to have static mock type", since we are doing overrides. I think we can just close it.

Nikzed commented 10 months ago

Anyway, it looks like that's an instance of "you must declare your mocks to have static mock type", since we are doing overrides. I think we can just close it.

Yes, this is definitely was in generated file .mock