Skip to content

Mock isn't a valid concrete implementation of FirebaseFirestore #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bynicodevelop opened this issue Mar 11, 2021 · 1 comment
Closed

Comments

@bynicodevelop
Copy link

Hello,

I integrated the latest version of Mockito into the saint of one of my apps.

I have a case where I am using Mockito with Flutter's FirebaseFirestore.

Here is my test case:

import 'package:app/models/private_tip_model.dart';
import 'package:app/repositories/private_tips_repository.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';

main() {
  test("Doit permettre de retourer une liste de conseils par ordre de création",
      () async {
    // ARRANGE
    FirebaseFirestoreMock firebase = FirebaseFirestoreMock();
    CollectionReferenceMock collectionReferenceMock = CollectionReferenceMock();
    QueryMock queryMock = QueryMock();
    QuerySnapshotMock querySnapshotMock = QuerySnapshotMock();
    QueryDocumentSnapshotMock queryDocumentSnapshotMock = QueryDocumentSnapshotMock();

    when(queryDocumentSnapshotMock.id).thenReturn('0987654321');
    when(queryDocumentSnapshotMock.data()).thenReturn({
      'title': 'Tips 1',
      'content': 'Content 1',
      'createdAt': 1234567,
    });

    when(querySnapshotMock.docs).thenReturn([queryDocumentSnapshotMock]);

    when(queryMock.get())
        .thenAnswer((realInvocation) => Future.value(querySnapshotMock));

    when(collectionReferenceMock.orderBy(
      'createdAt',
      descending: true,
    )).thenReturn(queryMock);

    when(firebase.collection(PrivateTipsRepository.COLLECTION))
        .thenReturn(collectionReferenceMock);

    final PrivateTipsRepository privateTipsRepository =
        PrivateTipsRepository(firebase);

    // ACT
    List<PrivateTipModel> privateTips = await privateTipsRepository.tips;

    // ASSERT
    expect(privateTips.length, 1);
    expect(privateTips[0].uid, '0987654321');
    expect(privateTips[0].title, 'Tips 1');
    expect(privateTips[0].content, 'Content 1');
    expect(privateTips[0].createdAt, 1234567);
  });
}

class FirebaseFirestoreMock extends Mock implements FirebaseFirestore {}

// ignore: must_be_immutable
class CollectionReferenceMock extends Mock implements CollectionReference {}

class QuerySnapshotMock extends Mock implements QuerySnapshot {}

class QueryDocumentSnapshotMock extends Mock implements QueryDocumentSnapshot {}

class QueryMock extends Mock implements Query {}

Visual studio code shows me the following error on the class FirebaseFirestoreMock:

Capture d’écran 2021-03-11 à 16 17 48

I understood well in your documentation that it was now necessary to create methods in the mocks.

But even if I create the method used in my test (collection), I still get this error.

Thanks.

@srawlins
Copy link
Member

Duplicate of #354

@srawlins srawlins marked this as a duplicate of #354 Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants