颤振测试块错误类型"Null"不是类型的子类型



我正在测试一个从api获取餐厅列表的block方法。

下面是我的代码:
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mobile_app_engineer/blocs/merchant_bloc.dart';
import 'package:mobile_app_engineer/repositories/merchant_repository.dart';
import 'package:mockito/mockito.dart';
import 'package:mobile_app_engineer/models/list_merchants_response.dart';
class MockMerchantRepository extends Mock implements MerchantRepository {}
// Generate a MockClient using the Mockito package.
// Create new instances of this class in each test.
void main() {
late MockMerchantRepository mockMerchantRepository;
setUp((){
mockMerchantRepository = MockMerchantRepository();
});
List<Merchant> merchantList = [
Merchant object here    
];
blocTest<MerchantBloc, MerchantState>(
'Check if merchant fetch return correct data',
build: () {
when(mockMerchantRepository.fetchMerchants(1)).thenThrow(NetworkError()); <=== in this line main 101
return MerchantBloc(mockMerchantRepository);
},
act: (bloc) => bloc.add(GetMerchantsList(1)),
expect: () => [MerchantInitial(), GetMerchantsListState(merchantList)],
);

}

我得到这个错误:

type 'Null' is not a subtype of type 'Future<List<Merchant>>'
package:mobile_app_engineer/repositories/merchant_repository.dart 14:26  
MockMerchantRepository.fetchMerchants
test/api_test/fetch_restaurant_test.dart 101:35                          main.<fn>
package:bloc_test/src/bloc_test.dart 176:25                              testBloc.<fn>

是我做错了什么吗?thx

找到了解决方案:这是因为null安全,所以现在我不得不添加GenerateMocks来生成api调用的模拟存储库。

import 'fetch_merchant_test.mocks.dart';
@GenerateMocks([MockMerchantRepository])
void main() {
final mockMerchantRepository = MockMockMerchantRepository();

相关内容

  • 没有找到相关文章

最新更新