【发布时间】: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