【问题标题】:Why does my test fail on verification of the mock function even though its been called already?为什么我的测试在验证模拟函数时失败,即使它已经被调用?
【发布时间】:2021-03-12 17:42:00
【问题描述】:

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

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 帐户创建一个帐户。

【问题讨论】:

    标签: flutter dart flutter-test


    【解决方案1】:

    你忘了等待expectLater:

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

    此外,如果您想在将来捕获此类错误,请查看 dart linting 选项。 unawaited_futures 会提醒您这一点

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

    【讨论】:

    • ? 你不会相信我尝试调试这么小的错误多久
    • 在我接受答案之前,请您回答关于signinwithCredentials的第二个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 2021-07-10
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多