简体   繁体   中英

Flutter Unit Test for a Firebase Function

I'm brand new to flutter and I want to try to make a Unit Test for one functions that I've created. The function simply test if the email & password that the user provides to login are correct or not. So, the function provides a connection to the database and verify if the email & password are valid.

I tried to make a Unit Test with mockito to emulate the Firebase but it don't work very well. My function return an "UserCredential" type and I don't know how to verify it with Mocks. I tried to create a Mock for this type, but it says that "type 'Null' is not a subtype of type 'Future'"...

Anyone can make a clear explanation? :)

Firebase Function to test

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flexmes/data/models/user_model.dart';

class UserAPI{
  final CustomUser _customUser = CustomUser();
  final FirebaseAuth auth;
  UserAPI({required this.auth});

  Future<UserCredential?> signInWithEmailAndPassword(String email, String password) async {
    try{
      UserCredential result =  await auth.signInWithEmailAndPassword(email: email, password: password);
      print (result);
      return result;
    } catch (error){
      print (error);
      return null;
    }
  }
}

Unit Test for the signInWithEmailAndPassword function

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flexmes/data/data_providers/user_provider.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';

class MockFirebaseAuth extends Mock implements FirebaseAuth{}
class MockUserCredential extends Mock implements UserCredential{}

void main(){
  final MockFirebaseAuth mockAuth = MockFirebaseAuth();
  final MockUserCredential userCredential = MockUserCredential();
  group('description', () {
    late UserAPI userAPI;
    
    setUp(() {
      userAPI = UserAPI(auth: mockAuth);
    });

    tearDown(() {
    });

    test('Sign in with Email & Password', () async {
      when(mockAuth.signInWithEmailAndPassword(email: "admin@admin.com", password: "password")).
      thenAnswer((_) => Future<MockUserCredential>.value(userCredential));
      expect(await userAPI.signInWithEmailAndPassword("admin@admin.com", "password"), userCredential);
    });
  });

}

There is mock authentication package available firebase_auth_mocks and google_sign_in_mocks

You can replace your code like this

group('description', () {
    final _mockAuth = MockFirebaseAuth(mockUser: _mockUser);
    late UserAPI userAPI;

    setUp(() {
      userAPI = UserAPI(auth: _mockAuth);
    });

    tearDown(() {});
    const _email = 'ilyas@yopmail.com';
    const _uid = 'sampleUid';
    const _displayName = 'ilyas';
    const _password = 'Test@123';
    final _mockUser = MockUser(
      uid: _uid,
      email: _email,
      displayName: _displayName,
    );

    test('signIn function test', () async {
      final user = await userAPI.signInWithEmailAndPassword(_email, _password);
      expect(user?.uid, _mockUser.uid);
    });
  });

google_sign_in_mocks this you can use for GoogleSignIn test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM