infinite_scroll_pagination icon indicating copy to clipboard operation
infinite_scroll_pagination copied to clipboard

Unable to mock PagingController with latest mockito and infinite_scroll_pagination

Open mlars84 opened this issue 3 years ago • 1 comments

Tools:

  • flutter 2.8.1
  • mockito ^5.0.17
  • infinite_scroll_pagination ^3.1.0

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 avatar Feb 08 '22 19:02 mlars84

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}),
  ],
)

mlars84 avatar Feb 08 '22 20:02 mlars84