-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/sitatec/firebase_database_m…
…ocks into main
- Loading branch information
Showing
1 changed file
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
||
|