Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ocks into main
  • Loading branch information
sitatec committed Dec 29, 2020
2 parents 70679d5 + 60e7676 commit cdcfbde
Showing 1 changed file with 69 additions and 3 deletions.
72 changes: 69 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,76 @@
# firebase_database_mocks
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.

A new Flutter project.
## 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';
## Getting Started
class UserRepository {
UserRepository(this.firebaseDatabase);
FirebaseDatabase firebaseDatabase;
This project is a starting point for a Flutter application.
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',
}),
);
});
}
```

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

Expand Down

0 comments on commit cdcfbde

Please sign in to comment.