EdsonBueno / infinite_scroll_pagination

Flutter package to help you lazily load and display pages of items as the user scrolls down your screen.
https://pub.dev/packages/infinite_scroll_pagination
MIT License
632 stars 214 forks source link

Unable to mock PagingController with latest mockito and infinite_scroll_pagination #169

Closed mlars84 closed 1 year ago

mlars84 commented 2 years ago

Tools:

Using mockito's GenerateMocks annotation, @GenerateMocks([PagingController]), results in the following error and build_runner failure:

Invalid @GenerateMocks annotation: Mockito cannot generate a valid mock class which implements 'PagingController' for the following reasons:
    The property accessor 'PagingController.firstPageKey' features a non-nullable unknown return type, and cannot be stubbed without a dummy generator specified on the MockSpec.

Any thoughts on a potential solution for this?

mlars84 commented 2 years ago

Workaround solution:

abstract class FirstPageKey {
  T firstPageKey<T>(T a, int b);
}

T firstPageKeyShim<T>(T a, int? b) {
  if (a is int) return a;
  throw 'unknown';
}

@GenerateMocks(
  [],
  customMocks: [
    MockSpec<PagingController>(
        returnNullOnMissingStub: true,
        fallbackGenerators: {#firstPageKey: firstPageKeyShim}),
  ],
)