flutterdartriverpod

How do I unit test for controller initial state when AsyncData is a List<Object>


I have a test testing for the initial state of a controller. It keeps failing saying no matching calls, but my other controller tests using the same code are fine (the difference is they have an AsyncValue type of void and a null default value whereas this is a list with an empty list default value, can someone please help? My test and controller code are below. The test finds 1 call which matches the same types as what I verify but for some reason, it just won't accept it. My test code is below:

Test file

test('initial state is AsyncValue.data', () async {
  // assert
  var service = MockSpaceService();
  var container = makeProviderContainer(service);
  var listener = Listener<AsyncValue<List<Space>>>();
  container.listen(spaceListControllerProvider, listener.call,
      fireImmediately: true);
  verify(
    () => listener(null, const AsyncData<List<Space>>([])),
  );
  verifyNoMoreInteractions(listener);
});

Controller file

@riverpod
class SpaceListController extends _$SpaceListController {
  @override
  FutureOr<List<Space>> build() => [];
}

Error

No matching calls. All calls: Listener<AsyncValue<List<Space>>>.call(null, AsyncData<List<Space>>(value: []))
(If you called `verify(...).called(0);`, please instead use `verifyNever(...);`.)
package:matcher                                                          fail
package:mocktail/src/mocktail.dart 728:7                                 _VerifyCall._checkWith
package:mocktail/src/mocktail.dart 519:18                                _makeVerify.<fn>
test\features\spaces\presentation\space_list_controller_test.dart 58:13  main.<fn>.<fn>

Solution

  • It is because [] != [] and const [] != []

    This should solve the issue:

    @riverpod
    class SpaceListController extends _$SpaceListController {
      @override
      FutureOr<List<Space>> build() => const [];
    }