firebase_database_mocks 0.6.0 icon indicating copy to clipboard operation
firebase_database_mocks: ^0.6.0 copied to clipboard

Fakes to write unit tests for FirebaseDatabase (real-time database).

firebase_database_mocks #

Pub Version test: passing codecov style: effective dart

A library that makes it easy to write unit tests for FirebaseDatabase (Realtime Database).

Usage #

Get an instance of MockFirebaseDatabase like this : MockFirebaseDatabase.instance, then use it in your tests as if it was the real FirebaseDatabase.instance.

By default the library keeps the data in memory as long as the tests are running, but you can disable the data persistence as follow: MockFirebaseDatabase.setDataPersistenceEnabled(ennabled: false);.

If the data persistence is disabled, each time you create an instance of MockDatabaseReference either by using the constructor: MockDatabaseReference(), or by getting the root reference on MockFirebaseDatabase instance : MockFirebaseDatabase.instance.ref() a new data store is created instead of using the cached one.

Note: The MockFirebaseDatabase.setDataPersistenceEnabled() function is currently experimental, so you might face some issues when you disable the data persitence.

Code Sample #

import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_database_mocks/firebase_database_mocks.dart';
import 'package:flutter_test/flutter_test.dart';

class UserRepository {
  FirebaseDatabase firebaseDatabase;

  Future<String> getUserName(String userId) async {
    final userNameReference =
    final dataSnapshot = await userNameReference.once();
    return dataSnapshot.value;

  Future<Map<String, dynamic>> getUser(String userId) async {
    final userNode = firebaseDatabase.reference().child('users/$userId');
    final dataSnapshot = await userNode.once();
    return dataSnapshot.value;

void main() {
  FirebaseDatabase firebaseDatabase;
  UserRepository userRepository;
  // Put fake data
  const userId = 'userId';
  const userName = 'Elon musk';
  const fakeData = {
    'users': {
      userId: {
        'name': userName,
        'email': '',
        'photoUrl': 'url-to-photo.jpg',
      'otherUserId': {
        'name': 'userName',
        'email': '',
        'photoUrl': 'other_url-to-photo.jpg',
  setUp(() {
    firebaseDatabase = MockFirebaseDatabase.instance;
    userRepository = UserRepository(firebaseDatabase);
  test('Should get userName ...', () async {
    final userNameFromFakeDatabase = await userRepository.getUserName(userId);
    expect(userNameFromFakeDatabase, equals(userName));

  test('Should get user ...', () async {
    final userNameFromFakeDatabase = await userRepository.getUser(userId);
        'name': userName,
        'email': '',
        'photoUrl': 'url-to-photo.jpg',

As you can see you don't need to initialize firebase core for testing or call TestWidgetsFlutterBinding.ensureInitialized() before using MockFirebaseDatabase, but if you use another firebase service that needs it, you can simply call the setupFirebaseMocks() top level function which performs all required operations for testing a firebase service that isn't fully mocked.

Supported getters/methods #

  • MockFirebaseDatabase
    • ref()
    • reference()
  • MockDatabaseReference
    • key
    • path
    • child()
    • get()
    • set()
    • update()
    • remove()
    • once() (DatabaseEventType.value only)
    • push()
  • MockDataSnapshot
    • key
    • ref
    • value
    • exists
    • hasChild
    • child
    • children

Contributing #

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.