我正在努力学习mockitohttp客户端测试。所以这是我的测试:
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))));
},
);
如你所见,我想扔
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>