当 AsyncData 是列表时,如何对控制器初始状态进行单元测试<Object>

问题描述 投票:0回答:1

我对控制器的初始状态进行了测试。它一直失败,说没有匹配的调用,但我使用相同代码的其他控制器测试很好(区别在于它们有一个 void 的 AsyncValue 类型和一个 null 默认值,而这是一个具有空列表默认值的列表,有人可以吗?我的测试和控制器代码如下。测试发现 1 个调用与我验证的类型匹配,但由于某种原因,它不接受它:

测试文件

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);
});

控制器文件

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

错误

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>
flutter dart riverpod
1个回答
0
投票

因为

[] != []
const [] != []

这应该可以解决问题:

@riverpod
class SpaceListController extends _$SpaceListController {
  @override
  FutureOr<List<Space>> build() => const [];
}
© www.soinside.com 2019 - 2024. All rights reserved.