Skip to content

Instantly share code, notes, and snippets.

@anargu
Created March 11, 2021 22:54
Show Gist options
  • Save anargu/b9e84bab7f5c10440b045f5f252f47e8 to your computer and use it in GitHub Desktop.
Save anargu/b9e84bab7f5c10440b045f5f252f47e8 to your computer and use it in GitHub Desktop.
Firebase Storage Test (Attempt)
// boring app code ...
Future<dynamic> thefuntion() async {
final thumbnailFile = File('some/path');
final uploadThumbnailTask = _storage
.ref('thumbnails/1234/something')
.putFile(thumbnailFile);
final uploadThumbnailSnapshot =
await uploadThumbnailTask.whenComplete(() => null);
final thumbnailURL = await uploadThumbnailSnapshot.ref.getDownloadURL();
}
// ...
class MockFirestore extends Mock implements FirebaseFirestore {}
class MockCollectionReference extends Mock implements CollectionReference {}
class MockDocumentReference extends Mock implements DocumentReference {}
class MockDocumentSnapshot extends Mock implements DocumentSnapshot {}
class MockFirestorage extends Mock implements FirebaseStorage {}
class MockStorageReference extends Mock implements Reference {}
class MockTaskSnapshot extends Mock implements TaskSnapshot {}
class MockUploadTask extends Mock implements UploadTask {}
void main() {
MockFirestore _firestore;
MockDocumentSnapshot mockDocumentSnapshot;
MockCollectionReference mockCollectionReference;
MockDocumentReference mockDocumentReference;
MockFirestorage _storage;
MockStorageReference _storageReference;
MockTaskSnapshot _taskSnapshot;
MockUploadTask _uploadTask;
setUp(() {
_firestore = MockFirestore();
_storage = MockFirestorage();
mockCollectionReference = MockCollectionReference();
mockDocumentReference = MockDocumentReference();
mockDocumentSnapshot = MockDocumentSnapshot();
_storageReference = MockStorageReference();
_taskSnapshot = MockTaskSnapshot();
_uploadTask = MockUploadTask();
});
test(
'given test',
() async {
final getDataMap = <String, dynamic>{
'tasks': [
{'title': 'task A', 'photos': []},
{'title': 'task B', 'photos': []}
]
};
when(_firestore.collection(any)).thenReturn(mockCollectionReference);
when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
when(mockDocumentReference.get())
.thenAnswer((_) async => mockDocumentSnapshot);
when(mockDocumentSnapshot.data()).thenReturn(getDataMap);
when(mockDocumentReference.set(any, any)).thenAnswer((_) async => {});
when(_storage.ref(any)).thenReturn(_storageReference);
when(_storageReference.putFile(any, any)).thenAnswer((_) => _uploadTask);
when(_storageReference.putFile(any)).thenAnswer((_) => _uploadTask);
when(_uploadTask.then(any)).thenAnswer((_) async => _taskSnapshot);
when(_uploadTask.whenComplete(any)).thenAnswer((_) async => _taskSnapshot);
when(_taskSnapshot.ref).thenReturn(_storageReference);
when(_storageReference.getDownloadURL()).thenAnswer((_) async => 'url');
final response = await thefuntion();
response.fold(
(l) => fail('test should not return failure. Failure: $l'),
(r) => expect(r, unit),
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment