【发布时间】:2020-08-05 02:20:18
【问题描述】:
我正在尝试学习 mockito http 客户端测试。所以这是我的测试:
test(
'should throw SocketException',
() async {
when(mockHttpCLient.get(any, headers: anyNamed('headers')))
.thenThrow(SocketException);
expect(
() => foursquareRepositoryImpl.getVenuesDetails(venueId),
throwsA(allOf(
isArgumentError, predicate((e) => e is FetchDataException))));
},
);
如您所见,当调用 http get 方法时,我想抛出 SocketException:
这个实现的方法:
@override
Future<VenuesDetails> getVenuesDetails(String venueId) async {
try {
var response = await client.get(
'https://api.foursquare.com/v2/venues/$venueId?client_id={{client_id}}&client_secret={{client_secret}}&v={{v}}',
headers: {'Content-Type': 'application/json; charset=utf-8'});
} on SocketException catch (e) {
throw FetchDataException('No Internet connection');
}
return null;
}
但是当我运行我的测试时,我得到了这个错误:
Expected: throws (<Instance of 'ArgumentError'> and satisfies function)
Actual: <Closure: () => Future<VenuesDetails>>
Which: threw FetchDataException:<Error During Communication: No Internet connection>
stack package:foursquare_app/repository/foursquare_repository.dart 60:7 FoursquareRepositoryImpl.getVenuesDetails
test/repository/foursquare_repository_impl_test.dart 157:23 main.<fn>.<fn>.<fn>
package:test_api expect
package:flutter_test/src/widget_tester.dart 234:3 expect
test/repository/foursquare_repository_impl_test.dart 156:9 main.<fn>.<fn>
===== asynchronous gap ===========================
package:test_api expect
package:flutter_test/src/widget_tester.dart 234:3 expect
test/repository/foursquare_repository_impl_test.dart 156:9 main.<fn>.<fn>
dart:async _AsyncAwaitCompleter.start
test/repository/foursquare_repository_impl_test.dart 138:7 main.<fn>.<fn>
package:test_api expect
package:flutter_test/src/widget_tester.dart 234:3 expect
test/repository/foursquare_repository_impl_test.dart 156:9 main.<fn>.<fn>
dart:async _AsyncAwaitCompleter.start
test/repository/foursquare_repository_impl_test.dart 138:7 main.<fn>.<fn>
我的错误在哪里?
【问题讨论】:
标签: unit-testing flutter mockito flutter-layout