How to mock/create a Firebase Usercredentials object for tests


how do I create a UserCredential Object when testing?

I am trying to test my authRepository like this :

      test('', () async {
          () => mockFirebaseAuth.createUserWithEmailAndPassword(
              email: '', password: '123456'),
        ).thenAnswer((realInvocation) => ) // I need to return a Future<UserCredential> here);
        final result = await authRepository.signUpWithEmailAndPassword(
            email: '', password: '123456');

but in order to stub createUserWithEmailAndPassword I need to return a

How do I create such an object?


Okay, I found a working solution. Turns out I had to mock it just like any other object:

class MockUserCredential extends Mock implements UserCredential {}

To modify this mock object, stubbing the needed property works as expected :

mockCredential = MockUserCredential();
when(() => mockCredential.user).thenReturn(_mockUser);

Answered By – Christian

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.