Coder Social home page Coder Social logo

lzyct / firebase_database_mocks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sitatec/firebase_database_mocks

0.0 0.0 0.0 73 KB

A testing library for firebase realtime database with flutter.

License: BSD 3-Clause "New" or "Revised" License

Dart 100.00%

firebase_database_mocks's Introduction

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 {
  UserRepository(this.firebaseDatabase);
  FirebaseDatabase firebaseDatabase;

  Future<String> getUserName(String userId) async {
    final userNameReference =
        firebaseDatabase.reference().child('users').child(userId).child('name');
    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': '[email protected]',
        'photoUrl': 'url-to-photo.jpg',
      },
      'otherUserId': {
        'name': 'userName',
        'email': '[email protected]',
        'photoUrl': 'other_url-to-photo.jpg',
      }
    }
  };
  MockFirebaseDatabase.instance.reference().set(fakeData);
  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);
    expect(
      userNameFromFakeDatabase,
      equals({
        'name': userName,
        'email': '[email protected]',
        '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.

firebase_database_mocks's People

Contributors

sitatec avatar zohenn avatar seyun avatar olegnovosad avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.