-
Notifications
You must be signed in to change notification settings - Fork 166
@GenerateMocks does not work on Generic method return types #338
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
Comments
Good question. It will be tricky, and I think we'll need to add new functionality. There are really two problems here. Looking at an example class: class C {
int get m => 0;
String s(String a) => a;
/// 100 other methods you'd like to stub...
T add<T>(T data) => data;
}
We can solve the problems separately. There are a few ways to solve the first:
I haven't really thought of solutions for the second problem. Maybe user-authored mixin would work here too, if it can somehow call |
Thanks for the detailed response @srawlins. I agree that problem 1. is the more common and probably the issue that will surface a lot when null safety becomes the new default. Solution 2. of using a Mixin to fall back to sounds good enough to handle such cases. I also agree that 3. does not seem to be user-friendly. The second problem is definitely the one I am stuck on right now. But more so in combination with Futures #339. I think those two problems are pretty similar. |
Regarding the second problem, I think you can even get real mock support. Here is a rough mockup showing how you can use class Mock {
noSuchMethod(Invocation i) {
print(i.positionalArguments);
print('mock.nsm');
return 6;
}
}
class C {
T f<T>(T obj) => obj;
}
mixin Mixin on Mock, C {
T f<T>(T obj) {
if (obj is int) {
return super.noSuchMethod(Invocation.genericMethod(#f, [int], [1]));
} else if (obj is double) {
return super.noSuchMethod(Invocation.genericMethod(#f, [double], [1.5]));
}
throw 'boo';
}
}
abstract class _MockC extends Mock implements C {}
class MockC extends _MockC with Mixin {}
void main() {
MockC c = MockC();
c.f(7);
c.f(7.5);
} |
Would it be feasible to allow defining a top-level method with a matching generic signature, and configure the mock to defer to that implementation when there is no stub? |
@natebosch very cool idea. I'm going to prototype this. I haven't been very happy with my mixin idea because of the generics issues. |
Do you have a workaround for problem 1? |
Right its not a solution yet. I prototyped the top-level method idea this weekend and hope to loop back with my results today. |
Any news or ETA please? |
This will likely land within the next 40 days. |
This is available now with Mockito 5.0.9. Please see the new section in the NULL_SAFETY_README: https://github.com/dart-lang/mockito/blob/master/NULL_SAFETY_README.md#fallback-generators |
Works well for me! thanks |
I read the readme and it suggests to add an mShim method. |
I updated to 5.0.9 and tried the following
but got the error |
Solution: |
Compilation of created mock file fails: Source code:
Created mock file:
Errors:
|
@eggnstone I would like to share with you my working setup (I only needed to use a few of the methods, but hopefully it is useful). I believe you will have to mock the CollectionReference<Map<String, dynamic>> as well.
|
Thank you @ngxingyu this is a good starting point! I however need the Using
I get
|
@eggnstone Actually would something like this suffice? https://gist.github.com/ngxingyu/51b0e40422fedb4253f57babecd3d999
|
@ngxingyu That works, thank you! |
New problems arise when the My first try was to build several |
I didn't actually face this issue, my approach is actually to create a MockDocumentSnapshot for each usecase, so there's no need to return different responses for the same snapshot. I've updated my gist to give a slightly more detailed example. |
This works perfectly for the mocked classes without the If you need to
then it does not work anymore. This maybe a bug with the
Try calling And preparing via |
@eggnstone Yikes I can reproduce this. Not great. |
@eggnstone I think I can only reproduce this when the two types being mocked are identical, like Mockito does not explicitly support generating two mocks for identical types... |
|
Hey there! Any updates on this? I'm facing the same problem with Firestore's QueryDocumentSnapshot |
This issue is outdated and contains several threads. Mockito 5.1.0 contains an alternative to Please open a new issue for any problem observed with Mockito >=5.1.0 which cannot be solved with |
Hey,
I tried to create a Mock for the following Function:
Several Classes are extending
Base
.flutter pub run build_runner build
throws the following errorThe method 'Api.add' features a non-nullable unknown return type, and cannot be stubbed.
I am using Mockito: ^5.0.0-nullsafety.7
Any way to get around this?
The text was updated successfully, but these errors were encountered: