Mockito when()。thenAnswer()不嘲笑未来

时间:2019-07-14 13:12:00

标签: unit-testing flutter dart mockito

我有一个存储库类,它通过调用另一个返回相同Future的类的另一个函数来返回Future。而且我正尝试使用像这样的Mockito when().thenAnswer()函数来模拟存储库类

 when(mockAccountRepository.authenticateWithEmail(
    email: email,
    password: password,
  )).thenAnswer((_) => Future.value(SignedIn(token: token)));

存储库类具有返回Future的功能

Future<AccountEvent> authenticateWithEmail({
@required String email,
@required String password,
  }) {
    return _accountApiProvider.authenticateWithEmail(
      email: email,
      password: password,
    );
  }

accountApiProvider类具有以下功能,该功能也返回相同的Future

Future<AccountEvent> authenticateWithPhone(
      {@required String phone, @required String password}) async {
    final String data = '''{"phone":"$phone","password":"$password"}''';

    try {
      final Response response = await _dio.post('url', data: data);

      if (response.statusCode == 200) {
        print(response.data);
        final String token = response.data.accessToken;
        return SignedIn(token: token);
      } else {
        return SignInError('Internal Server Error');
      }
    } on DioError catch (e) {
      return SignInError(e.message);
    }
  }

因此,当我运行测试时,它不会返回thenAnswer()函数中提供的确切模拟值。

0 个答案:

没有答案