我正在学习TDD并练习我的测试写作。我想通过Firebase为我的登录写一个测试。这是测试文件:
import 'package:firebase/firebase.dart';
import 'package:firebase_auth/firebase_auth.dart' as fa;
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:scopetik/features/login/data/login_data_impl.dart';
import 'package:scopetik/features/login/models/user_model.dart';
class MockFirebaseAuth extends Mock implements fa.FirebaseAuth {}
class MockFirebaseUser extends Mock implements fa.User {}
class MockAuthResult extends Mock implements fa.UserCredential {}
void main() {
late LoginDataImpl sut;
late MockFirebaseAuth mockFirebaseAuth;
setUp(() {
mockFirebaseAuth = MockFirebaseAuth();
sut = LoginDataImpl(mockFirebaseAuth);
});
group(
'Test LoginDataImpl class',
() {
when(mockFirebaseAuth.signInWithEmailAndPassword(
email: 'email', password: 'password'))
.thenAnswer((_) async {
return MockAuthResult();
});
test(
"get UserModel",
() async {
//arrange
},
);
},
);
}
我不知道为什么when函数不让我返回future<UserCredential>
。
我也遇到了同样的问题。要做到这一点,您需要使用mockito,而不是mocktail。
你可以在这里找到这两个库的when
属性,mocktail,mockito