Mockito http.Client 类型 'Null' 不是类型转换中类型 'Future<Response>' 的子类型
Mockito http.Client type 'Null' is not a subtype of type 'Future<Response>' in type cast
我正在使用 Mockito 对 Flutter 应用程序中的 http 请求进行单元测试。
待测代码:
@GenerateMocks([http.Client])
void main() {
late GdprDataSourceImpl dataSource;
late MockClient mockHttpClient;
setUp(() {
mockHttpClient = MockClient();
dataSource = GdprDataSourceImpl(
httpClient: mockHttpClient,
baseUrl: '',
);
});
group('post Gdpr', () {
const status = true;
final url = Uri.parse('/gdpr/consent');
const body = {
'status': status,
};
final errorMessage = fixture('error_response.json');
test('should perform a POST request to /auth/otp', () async {
when(mockHttpClient.post(url, body: body)).thenAnswer(
(_) async => http.Response('', 201),
);
await dataSource.postGdpr(status: status);
verify(mockHttpClient.post(url, body: body));
});
});
}
我收到以下错误:
type 'Null' 不是类型 cast
中类型 'Future' 的子类型
已使用构建运行程序成功生成模拟
我还尝试更新模拟的 class 并将 return 值更改为
returnValue: Future<http.Response?>
我得到:type 'Future<Response?>' is not a subtype of type 'Future' in type cast
所以解决方案是使用 package:http 提供的 MockClient class,
并且不使用 mockito。
group('post Gdpr success', () {
Future<http.Response> requestHandler(http.Request request) async {
final apiResponse = fixture('gdpr_response.json');
return http.Response(apiResponse, 201);
}
setUp(() {
mockHttpClient = MockClient(requestHandler);
dataSource = GdprDataSourceImpl(
httpClient: mockHttpClient,
baseUrl: '',
);
});
test('should perform a POST request to /gdpr/consent', () async {
final result = await dataSource.postGdpr(status: status);
expect(result, gdprResponse);
});
});
我正在使用 Mockito 对 Flutter 应用程序中的 http 请求进行单元测试。
待测代码:
@GenerateMocks([http.Client])
void main() {
late GdprDataSourceImpl dataSource;
late MockClient mockHttpClient;
setUp(() {
mockHttpClient = MockClient();
dataSource = GdprDataSourceImpl(
httpClient: mockHttpClient,
baseUrl: '',
);
});
group('post Gdpr', () {
const status = true;
final url = Uri.parse('/gdpr/consent');
const body = {
'status': status,
};
final errorMessage = fixture('error_response.json');
test('should perform a POST request to /auth/otp', () async {
when(mockHttpClient.post(url, body: body)).thenAnswer(
(_) async => http.Response('', 201),
);
await dataSource.postGdpr(status: status);
verify(mockHttpClient.post(url, body: body));
});
});
}
我收到以下错误: type 'Null' 不是类型 cast
中类型 'Future' 的子类型已使用构建运行程序成功生成模拟
我还尝试更新模拟的 class 并将 return 值更改为
returnValue: Future<http.Response?>
我得到:type 'Future<Response?>' is not a subtype of type 'Future' in type cast
所以解决方案是使用 package:http 提供的 MockClient class, 并且不使用 mockito。
group('post Gdpr success', () {
Future<http.Response> requestHandler(http.Request request) async {
final apiResponse = fixture('gdpr_response.json');
return http.Response(apiResponse, 201);
}
setUp(() {
mockHttpClient = MockClient(requestHandler);
dataSource = GdprDataSourceImpl(
httpClient: mockHttpClient,
baseUrl: '',
);
});
test('should perform a POST request to /gdpr/consent', () async {
final result = await dataSource.postGdpr(status: status);
expect(result, gdprResponse);
});
});