【发布时间】:2021-08-07 06:30:54
【问题描述】:
我正在尝试在我的项目中使用 mockito 来测试 API。这是organization_test.dart 下的一个小sn-p 代码。我只会提供直到错误行,因为它是唯一的问题。
class MockOrganizationRemoteDataSource extends Mock implements OrganizationRemoteDataSource {}
void main(){
late final MockOrganizationRemoteDataSource mockOrganizationDataSource;
late final IOrganizationRepository iOrganizationRepository;
final tOrgName = "someOrgName";
setUp((){
mockOrganizationRemoteDataSource = MockOrganizationRemoteDataSource();
iOrganizationRepository = IOrganizationRepository(mockOrganizationRemoteDataSource);
});
test("Should fetch the organization",() async {
when(mockOrganizationRemoteDataSource.getOrganization(tOrgName)) // Getting ERROR on this line
.thenAnswer(
(_) async => Response(
requestOption: RequestOption(
path: <Some url in here ????????>
data: <Some json response here ????????>
responseType: ResponseType.json)
),
);
final result = await iOrganizationRepository.fetchOrganization(tOrgName);
...
... // some more code here
});
}
然后我得到错误是type 'Null' is not a subtype of type 'Future<Response<dynamic>>'
我期待如果我使用 mock,我可以从 mockedDataSource 调用 getOrganization 方法并假装回答响应。然而,在调试时,我总是最终引用了 Un-mocked 类,即 OrganizationRemoteDataSource 导致 null 值。
顺便说一句,我正在使用启用了 null-safety 和 Dio 的颤振。
【问题讨论】:
-
您是否按照无效安全说明并致电
@GenerateMocks? -
显然,我没有也不知道。现在会检查。谢谢!我会再次询问我是否仍然遇到同样的错误。
-
谢谢@jamesdlin!它使用@GenerateMocks 工作。
标签: flutter unit-testing mockito dio