I’m new to unit test in flutter.
I’m trying to unit test a class.
This is my code:
class LoginRepository{
Future<SignInResult> signIn(
String email, String password) async {
try {
final result =
await Amplify.Auth.signIn(username: email, password: password);
return Right(result);
} catch (e) {
print('error sign in');
}
}
}
I need to write unit tests for signIn function.
group('Test Amplify Cognito', () {
TestWidgetsFlutterBinding.ensureInitialized();
setUp(() async {
try {
final auth = MockAmplifyAuthCognito();
await MockAmplifyClass().addPlugin(auth);
var varv = ConfigAppStaging().amplifyConfig;
await MockAmplifyClass().configure(jsonEncode(varv));
safePrint('successo');
} on PlatformException catch (e) {
safePrint('An error occured configuring amplify ${e.toString()}');
}
});
test('Sign In success', () async {
mockUserRepository = MockUserRepository();
mockAuthCategory = MockAuthCategory();
mockSignInResult = MockSignInResult();
when(() => MockAmplifyClass().Auth).thenReturn(mockAuthCategory);
when(() async => await mockAuthCategory.signIn(username: 'mail', password: 'password'))
.thenAnswer((_) async => mockSignInResult);
final results = await LoginRepository().signIn('mail', 'password');
var value = results.fold((l) => l, (r) => r);
expect(value, isA<SignInResult>());
});
});
I tried to test configuring amplify in setUp method and then mocking dependencies with mocktail package, but it doesn’t seem to work.
When i configure amplify i get ‘PlatformChannel Exception’.
How should I go about performing this test? There is another way to mock classes in flutter without using mockito or mocktail?