How to mock/create a Firebase Usercredentials object for tests

Issue

how do I create a UserCredential Object when testing?

I am trying to test my authRepository like this :

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

but in order to stub createUserWithEmailAndPassword I need to return a
Future<UserCredential>

How do I create such an object?

Solution

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.