为什么我的测试在验证模拟函数时失败,即使它已经被调用?

Why does my test fail on verification of the mock function even though its been called already?

所以我 运行 在 flutter 中测试我的身份验证实现,我似乎遇到了一个相当奇怪的错误。这是我的测试:

group('When remotedatabase calls signInWithGoogle', () {
group('and signs into google successfully', () {
  setUp(() {
    when(mockGoogleSignIn.signIn())
        .thenAnswer((realInvocation) async => mockGoogleSignInAccount);
  });
  test(
    'should return void if signin into firebase is successful',
    () async {
      // arrange

      when(mockFirebaseAuth.signInWithCredential(any))
          .thenAnswer((realInvocation) async => mockUserCredential);
      // act
      await remoteDatabaseImpl.signInWithGoogle();
      // assert
      verify(mockGoogleSignIn.signIn());
      //=> This verification passes <=//
      verify(mockFirebaseAuth.signInWithCredential(any));
    },
  );
  test(
    'should throw an authexception if unable to sign into firebase with credentials',
    () async {
      // arrange
      when(mockFirebaseAuth.signInWithCredential(any))
          .thenThrow(FirebaseAuthException(code: terror));
      // act
      final call = remoteDatabaseImpl.signInWithGoogle;

      // assert
      expectLater(call, throwsA(isA<AuthException>()));
      verify(mockGoogleSignIn.signIn());
      //=> This is the verification that fails <=//
      verify(mockFirebaseAuth.signInWithCredential(any));
    },
  );
});

 });

当我运行测试时,这是我的输出:

✓ When remotedatabase calls signInWithGoogle and signs into google successfully should return void if 
signin into firebase is successful
No matching calls (actually, no calls at all).
(If you called `verify(...).called(0);`, please instead use `verifyNever(...);`.)
package:test_api                                                                                 fail
_VerifyCall._checkWith
package:mockito/src/mock.dart:631
_makeVerify.<fn>
package:mockito/src/mock.dart:926
2
main.<fn>.<fn>.<fn>
test\…\auth_data_sources\auth_remote_database_impl_test.dart:123
2

✖ When remote database calls signInWithGoogle and signs into google successfully should throw an 
auth exception if unable to sign into firebase with credentials

这是我的实现:

@override
Future<void> signInWithGoogle() async {
    AuthCredential authCredential;
    // signin with google first
    await googleSignIn.signIn().then(
          (GoogleSignInAccount googleAccount) async =>
              await googleAccount.authentication.then(
            (GoogleSignInAuthentication googleauth) async {
              authCredential = GoogleAuthProvider.credential(
                accessToken: googleauth.accessToken,
                idToken: googleauth.idToken,
              );
              // finally sign with firebaseauth
              try {
                await firebaseAuth.signInWithCredential(authCredential);
              } on FirebaseAuthException catch (e) {
                throw AuthException(e.code);
              }
            },
          ),
        );
    }

正如您所见,对于测试相同代码实现的不同测试,相同的验证似乎失败了,我似乎无法找出问题所在。请帮忙。

此外,在 await firebaseAuth.signInWithCredential(authCredential); 的情况下,如果凭据不存在,此函数是否会为凭据创建一个帐户?因为我似乎无法找到像 await firebaseAuth.createAccountWithCredential(authCredential); 这样的功能,如果它是新的,我需要为 google 创建一个帐户。

您忘记等待 expectLater:

expectLater(call, throwsA(isA<AuthException>()));

此外,如果您以后想捕获此类错误,请查看 dart linting 选项。 unawaited_futures 会提醒您这件事

至于第二部分,是的,如果用户还没有帐户,它会创建一个帐户。 这也是documented on the function itself