我对控制器的初始状态进行了测试。它一直失败,说没有匹配的调用,但我使用相同代码的其他控制器测试很好(区别在于它们有一个 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>
因为
[] != []
和const [] != []
这应该可以解决问题:
@riverpod
class SpaceListController extends _$SpaceListController {
@override
FutureOr<List<Space>> build() => const [];
}