Flutter:Mockito http客户端单元测试异常

Posted

技术标签:

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

我的错误在哪里?

【问题讨论】:

【参考方案1】:

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

...

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

...

...

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


...

【讨论】:

以上是关于Flutter:Mockito http客户端单元测试异常的主要内容,如果未能解决你的问题,请参考以下文章

关于单元测试的那些事儿,Mockito 都能帮你解决

Junit mockito

Flutter Mockito 测试模拟失败

用 Mockito Flutter 模拟 Hive

flutter mockito:如何调用存储库函数进行测试

Mock以及Mockito的使用