【问题标题】:Flutter: Mockito http client unit test exceptionFlutter:Mockito http客户端单元测试异常
【发布时间】: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


    【解决方案1】:

    只需更改下面的代码,就可以正常工作。

    ...
    
    expect(foursquareRepositoryImpl.getVenuesDetails(venueId),
            throwsA(isA<FetchDataException>()));
    
    ...
    

    ...
    
    try{
         await foursquareRepositoryImpl.getVenuesDetails(venueId);
    } catch (e) {
         expect(e, isA<FetchDataException>());
    }
    
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      相关资源
      最近更新 更多