Mockito - 在空安全迁移后存根一个方法 [英] Mockito - stub a method after the null-safety migration
本文介绍了Mockito - 在空安全迁移后存根一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在空安全之前,我可以简单地模拟 sendRequest(...) 方法:
Before the null safety I could simply mock up the sendRequest(...) method like that:
void stubBaseRepositorySendRequestResponse(String response) {
when(baseRepository.sendRequest(onGetData: anyNamed('onGetData')))
.thenAnswer((_) {
return Future<String>.value(response);
});
}
这个方法的签名在哪里:
where the signature of this method is:
Future<T> sendRequest<T>({required Future<T> Function() onGetData})
如何使用新的空安全版本的 mockito 做到这一点?我还能保持这个存根的通用特性并允许传入任何参数吗?
How can this be done with the new null-safe version of mockito? Can I still maintain the generic character of this stub and allow for any arguments to be passed in?
推荐答案
这可以通过 mocktail一>.更容易,没有 mockito 所需的代码生成.
This can be easily done with mocktail. A lot easier, without the code gen required by mockito.
先导入mocktail很重要:
Important to import mocktail first:
import 'package:mocktail/mocktail.dart';
然后创建一个我们想要模拟的类的模拟:
then create a mock of the class that we want to mock:
class MockBaseRepository extends Mock implements BaseRepository {}
void stubBaseRepositorySendRequestResponse(String response) {
when(() => baseRepository.sendRequest(
onGetData: any(named: 'onGetData', that: isNotNull))).thenAnswer((_) {
return Future<String>.value(response);
});
}
这篇关于Mockito - 在空安全迁移后存根一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文