From dbed4ffb8061219a0c9522015d0736b1f13c12e1 Mon Sep 17 00:00:00 2001 From: sitatec <62622509+sitatec@users.noreply.github.com> Date: Tue, 29 Dec 2020 10:30:36 -0500 Subject: [PATCH 1/3] Update README.md --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5cf76d3..f532ca4 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,47 @@ A new Flutter project. -## Getting Started +## Usage +```dart +import 'package:firebase_database/firebase_database.dart'; +import 'package:firebase_database_mocks/firebase_database_mocks.dart'; +import 'package:flutter_test/flutter_test.dart'; -This project is a starting point for a Flutter application. +class UserRepository { + UserRepository(this.firebaseDatabase); + FirebaseDatabase firebaseDatabase; + + Future getUserName(String userId) async { + final userNode = + firebaseDatabase.reference().child('users').child('$userId/name'); + final dataSnapshot = await userNode.once(); + return dataSnapshot.value; + } +} + +void main() { + FirebaseDatabase firebaseDatabase; + UserRepository userRepository; + // Put fake data + const userId = 'userId'; + const userName = 'Elon musk'; + MockFirebaseDatabase.instance + .reference() + .child('users') + .child(userId) + .child('name') + .set(userName); + setUp(() { + firebaseDatabase = MockFirebaseDatabase.instance; + userRepository = UserRepository(firebaseDatabase); + }); + test('Should contain value', () async { + final userNameFromFakeDatabase = userRepository.getUserName(userId); + expect(userNameFromFakeDatabase, equals(userName)); + }); +} + +``` A few resources to get you started if this is your first Flutter project: From 59ded6da5b2e8064cad3705c24bb2c8d1852dd0a Mon Sep 17 00:00:00 2001 From: sitatec <62622509+sitatec@users.noreply.github.com> Date: Tue, 29 Dec 2020 10:39:00 -0500 Subject: [PATCH 2/3] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f532ca4..0dd3c09 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # firebase_database_mocks - -A new Flutter project. +Fakes to write unit tests for FirebaseDatabase (real-time database). Get Instance +`MockFirebaseDatabase.instance`, then pass it around your project as if it were a +`FirebaseDatabase.instance`. This mock keep data in memory while test running. ## Usage ```dart From 60e7676612c986d2e771f21e0d5ca72bf93207fc Mon Sep 17 00:00:00 2001 From: sitatec <62622509+sitatec@users.noreply.github.com> Date: Tue, 29 Dec 2020 10:40:29 -0500 Subject: [PATCH 3/3] Update README.md --- README.md | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0dd3c09..56a0868 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,14 @@ class UserRepository { FirebaseDatabase firebaseDatabase; Future getUserName(String userId) async { - final userNode = - firebaseDatabase.reference().child('users').child('$userId/name'); + final userNameReference = + firebaseDatabase.reference().child('users').child(userId).child('name'); + final dataSnapshot = await userNameReference.once(); + return dataSnapshot.value; + } + + Future> getUser(String userId) async { + final userNode = firebaseDatabase.reference().child('users/$userId'); final dataSnapshot = await userNode.once(); return dataSnapshot.value; } @@ -27,20 +33,41 @@ void main() { // Put fake data const userId = 'userId'; const userName = 'Elon musk'; - MockFirebaseDatabase.instance - .reference() - .child('users') - .child(userId) - .child('name') - .set(userName); + const fakeData = { + 'users': { + userId: { + 'name': userName, + 'email': 'musk.email@tesla.com', + 'photoUrl': 'url-to-photo.jpg', + }, + 'otherUserId': { + 'name': 'userName', + 'email': 'othermusk.email@tesla.com', + 'photoUrl': 'other_url-to-photo.jpg', + } + } + }; + MockFirebaseDatabase.instance.reference().set(fakeData); setUp(() { firebaseDatabase = MockFirebaseDatabase.instance; userRepository = UserRepository(firebaseDatabase); }); - test('Should contain value', () async { - final userNameFromFakeDatabase = userRepository.getUserName(userId); + 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': 'musk.email@tesla.com', + 'photoUrl': 'url-to-photo.jpg', + }), + ); + }); } ```