Home > database >  How to mock/create a Firebase Usercredentials object for tests
How to mock/create a Firebase Usercredentials object for tests

Time:01-03

how do I create a UserCredential Object when testing?

I am trying to test my authRepository like this :

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

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

How do I create such an object?

CodePudding user response:

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);
  • Related