Skip to content

@GenerateMocks does not generate correct stub for generic Future return Types #339

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

Closed
MaxAuer opened this issue Feb 23, 2021 · 12 comments
Closed

Comments

@MaxAuer
Copy link

MaxAuer commented Feb 23, 2021

Hey,

I tried to generate a Mock for the following Function:

Future<T> update<T extends Base>(T data);

Several classes are extending Base.

flutter pub run build_runner build does output the following stub:

@override
  _i3.Future<T> update<T extends _i4.Base>(T? data) =>
      (super.noSuchMethod(Invocation.method(#update, [data]),
          returnValue: Future.value(null)) as _i3.Future<T>);

I then use the following when in my test to stub the function

when(api.update<Base>(any)).thenThrow(Exception());

Which gives the following error type 'Future<Null>' is not a subtype of type 'Future<Note>' in type cast. This does make sense as Future.value(null) can not be parsed to Future<T> which is not nullable.

The correct behavior should be a value that can be parsed to Future<T>.

@MaxAuer MaxAuer changed the title @GenerateMocks does not generate correct stub for generic Futures @GenerateMocks does not generate correct stub for generic Future return Types Feb 23, 2021
@hoc081098
Copy link

Same issue :(

@hoc081098
Copy link

hoc081098 commented Feb 27, 2021

Trick: edit returnValue: Future.value(xxx) where xxx is value of testing type

@MaxAuer
Copy link
Author

MaxAuer commented Mar 6, 2021

Maybe worth checking this issue out: Mocktail-Type-Safety. @srawlins

He could solve this without any code generation. Sure it`s a breaking change but maybe its worth checking out for Mockito.

@srawlins
Copy link
Member

srawlins commented Mar 8, 2021

Mocktail uses strings for mocking, in which case you lose all type safety, typo protection, static analysis, etc. We will not be moving in that direction.

@MaxAuer
Copy link
Author

MaxAuer commented Mar 8, 2021

Sorry to contradict.

The issue I linked was opened by me because I saw the same issues with Mocktail. It was lacking type safety but works without code generation. Felix found a solution based on lambdas to have type safety without code generation and working with null safety.

He also proposed merging mockito into mocktail: #347

@kuhnroyal
Copy link
Contributor

I also have a lot of Future.value(null) return values generated that don't compile with strict analyzer settings.
This should be generated as Future<T>.value(null) to work with all kinds of analysis_options.yaml configurations.

@srawlins
Copy link
Member

@MaxAuer sorry I got my projects mixed up.

@srawlins
Copy link
Member

@kuhnroyal can you elaborate? analysis_options.yaml does not affect compilation.

@kuhnroyal
Copy link
Contributor

@srawlins You are correct. What I meant to say is, that with strong-mode and language settings on, the files are red in the IDE as these are not analyzer warnings but errors.

analyzer:
  language:
    strict-inference: true
    strict-raw-types: true
  strong-mode:
    implicit-dynamic: false
    implicit-casts: false

I know that I can exclude the generated files from analysis but it would be nice if the generated code conforms to these setting as it will then also conform to less strict settings. This would make it easier to add to projects.

@srawlins
Copy link
Member

Thanks @kuhnroyal . That sounds like a separate issue from this issue. I can take a look if you file a separate issue, and include the new static errors you are seeing.

@srawlins
Copy link
Member

A fix for this issue is available now with Mockito 5.0.9, "fallback generators". Please see the new section in the NULL_SAFETY_README:

https://github.com/dart-lang/mockito/blob/master/NULL_SAFETY_README.md#fallback-generators

@tomwyr
Copy link

tomwyr commented Jun 1, 2021

@srawlins Correct me if I'm wrong here, but since we don't care about actual results of Future objects and fallback generators serve only to make generated code null-safe, couldn't Mockito just return Completer<T>().future?

In a similar way methods that return generic streams are currently mocked. The difference here is that instead of a stream that never emits we just want a future that never completes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants