felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.82k stars 3.39k forks source link

[Question] Using mocks generated with mockito with whenListen #3250

Closed wujek-srujek closed 2 years ago

wujek-srujek commented 2 years ago

Returning to Flutter dev after a hiatus and I'm trying to write a widget test for a widget that uses a cubit. For this I use a mock generated with mockito (for nullsafety) and try to stub it using whenListen from bloc_test 8.1.x, and it fails (e.g. it says that .state stubbing is missing). I took a peek into the implementation of this function and it uses mocktail internally.

Question: I guess whenListen is not supposed to be used with mockito mocks, right, as they are incompatible? I tried looking in the docs for this information but couldn't find anything. If this is the case, could this be added somewhere? If I'm wrong and it is fine to use whenListen with mockito mocks, could you please show an example of doing it for a widget test?

felangel commented 2 years ago

Hi @wujek-srujek 👋 Thanks for opening an issue!

Unfortunately, bloc_test uses mocktail instead of mockito so when using bloc_test specific APIs you'll need to use mocktail. This is described in the migration guide but I'm happy to update the testing section of the documentation to make this more explicit. Let me know if that helps and sorry for the inconvenience!

wujek-srujek commented 2 years ago

No worries, not a problem, it is easy enough to mock ourselves and we are actually thinking of migrating to mocktail (any idea when it reaches final?).

I somehow missed the mention in the docs, thanks for the link. For now it only says that mocktail is used but it doesn't say how and what it really means - I think it would still make sense to be very explicit about mockito mocks not being supported by bloc_test and mentioning that we either have to migrate our mocking code tomocktail or whenListen etc. will not work as it used to. What do you think?