Skip to content

Instantly share code, notes, and snippets.

@kmartins
Forked from utkarsh-UK/firestore_all_mocks.dart
Created January 19, 2021 12:46
Show Gist options
  • Save kmartins/b6cb9ea9d7ceeb8b2c9acae4669097e7 to your computer and use it in GitHub Desktop.
Save kmartins/b6cb9ea9d7ceeb8b2c9acae4669097e7 to your computer and use it in GitHub Desktop.

Revisions

  1. @utkarsh-UK utkarsh-UK revised this gist Nov 19, 2020. 1 changed file with 71 additions and 71 deletions.
    142 changes: 71 additions & 71 deletions firestore_all_mocks.dart
    Original file line number Diff line number Diff line change
    @@ -20,84 +20,84 @@ void main() {
    mockDocumentReference = MockDocumentReference();
    mockDocumentSnapshot = MockDocumentSnapshot();
    });

    //Get data from firestore doc
    test('should return data when the call to remote source is successful.', () async {
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.get()).thenAnswer((_) async => mockDocumentSnapshot);
    when(mockDocumentSnapshot.data()).thenReturn(responseMap);
    //act
    final result = await remoteDataSource.getData('user_id');
    //assert
    expect(result, userModel); //userModel is a object that is defined. Replace with you own model class object.
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.get()).thenAnswer((_) async => mockDocumentSnapshot);
    when(mockDocumentSnapshot.data()).thenReturn(responseMap);
    //act
    final result = await remoteDataSource.getData('user_id');
    //assert
    expect(result, userModel); //userModel is a object that is defined. Replace with you own model class object.
    });

    //Add data to firestore doc
    //This demonstrates chained call like (instance.collection('path1').doc('doc1').collection('path2').doc('doc2').add(data));
    //We return the same mocked objects for each call.
    test('should get data from nested document.', () async {
    // arrange
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.add(any)).thenAnswer((_) async => mockDocumentReference);
    when(mockDocumentReference.id).thenReturn(messageID);
    when(mockDocumentSnapshot.data()).thenReturn(messageData);
    //act
    final result = await remoteDataSource.addData(userID, notificationID);
    //assert
    expect(result, notificationID);
    // arrange
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.add(any)).thenAnswer((_) async => mockDocumentReference);
    when(mockDocumentReference.id).thenReturn(messageID);
    when(mockDocumentSnapshot.data()).thenReturn(messageData);
    //act
    final result = await remoteDataSource.addData(userID, notificationID);
    //assert
    expect(result, notificationID);
    });

    //Get list of documents from firestore.
    test('should get list of all the documents.', () async {
    // arrange
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.get()).thenAnswer((_) async => mockDocumentSnapshot);
    when(mockDocumentSnapshot.data()).thenReturn(roomsMap);
    when(mockDocumentReference.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.get()).thenAnswer((_) async => mockQuerySnapshot);
    when(mockQuerySnapshot.docs).thenReturn([mockQueryDocumentSnapshot]);
    when(mockQueryDocumentSnapshot.data()).thenReturn(membersMap);
    //act
    final result = await remoteDataSource.fetchUsers();
    //assert
    expect(result, users);
    });

    //Get list of documents from firestore.
    test('should get list of all the documents.', () async {
    // arrange
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.get()).thenAnswer((_) async => mockDocumentSnapshot);
    when(mockDocumentSnapshot.data()).thenReturn(roomsMap);
    when(mockDocumentReference.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.get()).thenAnswer((_) async => mockQuerySnapshot);
    when(mockQuerySnapshot.docs).thenReturn([mockQueryDocumentSnapshot]);
    when(mockQueryDocumentSnapshot.data()).thenReturn(membersMap);
    //act
    final result = await remoteDataSource.fetchUsers();
    //assert
    expect(result, users);

    //Delete doc in firestore.
    test('should delete a doc and return true.', () async {
    // arrange
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.delete()).thenAnswer((_) async => Future.value());
    //act
    final result = await remoteDataSource.joinSquad();
    //assert
    expect(result, true);
    });

    //Mock query methods like orderBy(), limit().
    test('should return previous messages when getting data is successful.', () async {
    // arrange
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.collection('messages')).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(lastMessageID)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.get()).thenAnswer((realInvocation) async => mockDocumentSnapshot);
    when(mockCollectionReference.orderBy('timestamp', descending: true)).thenReturn(mockQuery);
    when(mockQuery.startAfterDocument(mockDocumentSnapshot)).thenReturn(mockQuery);
    when(mockQuery.limit(20)).thenReturn(mockQuery);
    when(mockQuery.get()).thenAnswer((_) async => mockQuerySnapshot);
    when(mockQuerySnapshot.docs).thenReturn([mockQueryDocumentSnapshot]);
    when(mockQueryDocumentSnapshot.data()).thenReturn(message);
    //act
    final result = await remoteDataSource.loadPreviousMessages(roomID: roomID, lastFetchedMessageID: lastMessageID);
    //assert
    expect(result, messages);
    });

    //Delete doc in firestore.
    test('should delete a doc and return true.', () async {
    // arrange
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.delete()).thenAnswer((_) async => Future.value());
    //act
    final result = await remoteDataSource.joinSquad();
    //assert
    expect(result, true);
    });

    //Mock query methods like orderBy(), limit().
    test('should return previous messages when getting data is successful.', () async {
    // arrange
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.collection('messages')).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(lastMessageID)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.get()).thenAnswer((realInvocation) async => mockDocumentSnapshot);
    when(mockCollectionReference.orderBy('timestamp', descending: true)).thenReturn(mockQuery);
    when(mockQuery.startAfterDocument(mockDocumentSnapshot)).thenReturn(mockQuery);
    when(mockQuery.limit(20)).thenReturn(mockQuery);
    when(mockQuery.get()).thenAnswer((_) async => mockQuerySnapshot);
    when(mockQuerySnapshot.docs).thenReturn([mockQueryDocumentSnapshot]);
    when(mockQueryDocumentSnapshot.data()).thenReturn(message);
    //act
    final result = await remoteDataSource.loadPreviousMessages(roomID: roomID, lastFetchedMessageID: lastMessageID);
    //assert
    expect(result, messages);
    });
    }
    }
  2. @utkarsh-UK utkarsh-UK created this gist Nov 19, 2020.
    103 changes: 103 additions & 0 deletions firestore_all_mocks.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    import 'package:cloud_firestore/cloud_firestore.dart';

    class MockFirestore extends Mock implements FirebaseFirestore {}

    class MockCollectionReference extends Mock implements CollectionReference {}

    class MockDocumentReference extends Mock implements DocumentReference {}

    class MockDocumentSnapshot extends Mock implements DocumentSnapshot {}

    void main() {
    MockFirestore instance;
    MockDocumentSnapshot mockDocumentSnapshot;
    MockCollectionReference mockCollectionReference;
    MockDocumentReference mockDocumentReference;

    setUp(() {
    instance = MockFirestore();
    mockCollectionReference = MockCollectionReference();
    mockDocumentReference = MockDocumentReference();
    mockDocumentSnapshot = MockDocumentSnapshot();
    });

    //Get data from firestore doc
    test('should return data when the call to remote source is successful.', () async {
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.get()).thenAnswer((_) async => mockDocumentSnapshot);
    when(mockDocumentSnapshot.data()).thenReturn(responseMap);
    //act
    final result = await remoteDataSource.getData('user_id');
    //assert
    expect(result, userModel); //userModel is a object that is defined. Replace with you own model class object.
    });

    //Add data to firestore doc
    //This demonstrates chained call like (instance.collection('path1').doc('doc1').collection('path2').doc('doc2').add(data));
    //We return the same mocked objects for each call.
    test('should get data from nested document.', () async {
    // arrange
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.add(any)).thenAnswer((_) async => mockDocumentReference);
    when(mockDocumentReference.id).thenReturn(messageID);
    when(mockDocumentSnapshot.data()).thenReturn(messageData);
    //act
    final result = await remoteDataSource.addData(userID, notificationID);
    //assert
    expect(result, notificationID);
    });

    //Get list of documents from firestore.
    test('should get list of all the documents.', () async {
    // arrange
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.get()).thenAnswer((_) async => mockDocumentSnapshot);
    when(mockDocumentSnapshot.data()).thenReturn(roomsMap);
    when(mockDocumentReference.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.get()).thenAnswer((_) async => mockQuerySnapshot);
    when(mockQuerySnapshot.docs).thenReturn([mockQueryDocumentSnapshot]);
    when(mockQueryDocumentSnapshot.data()).thenReturn(membersMap);
    //act
    final result = await remoteDataSource.fetchUsers();
    //assert
    expect(result, users);
    });

    //Delete doc in firestore.
    test('should delete a doc and return true.', () async {
    // arrange
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.delete()).thenAnswer((_) async => Future.value());
    //act
    final result = await remoteDataSource.joinSquad();
    //assert
    expect(result, true);
    });

    //Mock query methods like orderBy(), limit().
    test('should return previous messages when getting data is successful.', () async {
    // arrange
    when(instance.collection(any)).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(any)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.collection('messages')).thenReturn(mockCollectionReference);
    when(mockCollectionReference.doc(lastMessageID)).thenReturn(mockDocumentReference);
    when(mockDocumentReference.get()).thenAnswer((realInvocation) async => mockDocumentSnapshot);
    when(mockCollectionReference.orderBy('timestamp', descending: true)).thenReturn(mockQuery);
    when(mockQuery.startAfterDocument(mockDocumentSnapshot)).thenReturn(mockQuery);
    when(mockQuery.limit(20)).thenReturn(mockQuery);
    when(mockQuery.get()).thenAnswer((_) async => mockQuerySnapshot);
    when(mockQuerySnapshot.docs).thenReturn([mockQueryDocumentSnapshot]);
    when(mockQueryDocumentSnapshot.data()).thenReturn(message);
    //act
    final result = await remoteDataSource.loadPreviousMessages(roomID: roomID, lastFetchedMessageID: lastMessageID);
    //assert
    expect(result, messages);
    });
    }