g123k / flutter_app_badger

Support to update the app badge on the launcher (both for Android and iOS)
https://pub.dev/packages/flutter_app_badger
Apache License 2.0
307 stars 172 forks source link

feat: add setMocks method for use in testing #73

Closed riscait closed 1 year ago

riscait commented 1 year ago

When testing, there was no way to use mocks, so I implemented them.

There may be a better implementation, in which case I would appreciate your advice.

chadpav commented 1 year ago

This solves a problem that I have with unit testing and the code looks reasonable to me. I pulled in the fork and have it working in my app now. This is a fork of the current release (as of this post). Do we have a maintainer?

For reference, I had an error message when running unit tests after importing the flutter_app_badger package. The message was Binding has not yet been initialized. and contained the app badger in the stack trace. By mocking out the app badger using the fork I was able to pass all tests.

Here is the code I used to mock out the library inside the setup() method of my unit tests:

    FlutterAppBadger.setMocks(
      isAppBadgeSupported: () => Future.value(false),
      updateBadgeCount: (count) => Future.value(null),
    );

Since this is a mock you should change the values to whatever your code is expecting.

Thank you, @riscait

g123k commented 1 year ago

Thanks for your PR @riscait