How to mock the FirebaseApp in flutter

后端 未结 3 700
难免孤独
难免孤独 2021-01-22 09:32

I am trying to test a method that uses FirebaseFirestore but I am not able to mock the FirebaseFirestore.instance property.

I am following these examples:

  1. In
相关标签:
3条回答
  • 2021-01-22 09:40

    Maybe the cloud_firestore_mocks package is useful for you:

    Fakes to write unit tests for Cloud Firestore. Instantiate a MockFirestoreInstance, then pass it around your project as if it were a FirestoreInstance. This fake acts like Firestore except it will only keep the state in memory. To help debug, you can use MockFirestoreInstance.dump() to see what's in the fake database. This is useful to set up the state of your database, then check that your UI behaves the way you expect.

    Example from the docs:

    import 'package:cloud_firestore_mocks/cloud_firestore_mocks.dart';
    
    void main() {
      final instance = MockFirestoreInstance();
      await instance.collection('users').add({
        'username': 'Bob',
      });
      final snapshot = await instance.collection('users').get();
      print(snapshot.documents.length); // 1
      print(snapshot.documents.first['username']); // 'Bob'
      print(instance.dump());
    }
    
    0 讨论(0)
  • 2021-01-22 09:45

    I had the same problem. Using this answer I found the solution.

    1. Copy the contents of: https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_auth/firebase_auth/test/mock.dart into a file, that you can import into your tests, where you need to initialize a Firebase app.

    2. Call setupFirebaseAuthMocks(); at the top of your main function for all your tests.

    3. In your setUpAll function, call await Firebase.initializeApp(); (You can also put this in your main function under setupFirebaseAuthMocks(); will still work).

    Now you should have a mocked Firebase app.

    Here is a full example:

    import 'package:flutter/material.dart';
    import 'package:firebase_core/firebase_core.dart';
    import 'package:flutter_test/flutter_test.dart';
    import '../lib/authentication/your_auth_using_firebase.dart';
    import './mock.dart'; // from: https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_auth/firebase_auth/test/mock.dart
    
    
    
    void main() {
      // TestWidgetsFlutterBinding.ensureInitialized(); Gets called in setupFirebaseAuthMocks()
      setupFirebaseAuthMocks();
    
      setUpAll(() async {
        await Firebase.initializeApp();
      });
    
      testWidgets('Your Test', (WidgetTester tester) async {
        final YourFirebaseAuthClass authService = YourFirebaseAuthClass();
        // Tests to write
      });
    }
    
    0 讨论(0)
  • 2021-01-22 09:54

    think that is for use FutureBuilder as first Widget o root widget of the tree, flutter needs always a MaterialApp or others(dont remmember right now).

    Recomendation, refactor FutureBuilder -> Wrap whit widget -> MaterialApp(home:FutureBuilder(...

    and check MyWidget() first widget, maybe same MaterialApp get error, but not sure.

    if you are using routes method to navigate use initialRoute.

    Luck!

    0 讨论(0)
提交回复
热议问题