如何在扑朔迷离中模拟FirebaseApp [英] How to mock the FirebaseApp in flutter

查看:53
本文介绍了如何在扑朔迷离中模拟FirebaseApp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试使用FirebaseFirestore的方法,但无法模拟FirebaseFirestore.instance属性.

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

我正在关注以下示例:

  1. 初始化核心: https://firebase.flutter.dev/docs/overview#Initializing-flutterfire
  2. 使用firestore插件: https://firebase.flutter.dev/docs/firestore/usage

我正在为我的课程使用下面的代码,并且运行良好,这意味着firestoreInstance已正确加载

I am using the code below for my class and it is working well, which means the firestoreInstance is loaded correctly

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(
    Main(firebaseApp: Firebase.initializeApp()),
  );
}

class Main extends StatelessWidget {
  final Future<FirebaseApp> firebaseApp;
  const Main({this.firebaseApp});

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: firebaseApp,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          final firestoreInstance = FirebaseFirestore.instanceFor(
            app: snapshot.data,
          );
          return MyWidget();
        }
        return CircularProgressIndicator();
      },
    );
  }
}

但是当我运行下面的测试时,我得到了消息:

But when I run the test below I got the message:

",在构建Builder(dirty)时引发了以下FirebaseException:[core/no-app]尚未创建Firebase应用程序'[DEFAULT]'-调用Firebase.initializeApp()"

"The following FirebaseException was thrown building Builder(dirty): [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()"


import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';

class MockFirebaseApp extends Mock implements FirebaseApp {}

void main() {
  FirebaseApp firebaseApp;

  setUp(() async {
    TestWidgetsFlutterBinding.ensureInitialized();
    firebaseApp = MockFirebaseApp();
  });

  group('Main', () {
    testWidgets('Loads my widget', (WidgetTester tester) async {
      await tester.runAsync(() async {
        await tester.pumpWidget(
          Main(firebaseApp: Future.value(firebaseApp)),
        );

        expect(find.byType(CircularProgressIndicator), findsOneWidget);
      });
    });
  });
}

推荐答案

我遇到了同样的问题.使用此答案,我找到了解决方案.

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

  1. 复制以下内容: https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_auth/firebase_auth/test/mock.dart 到文件中,您可以将其导入测试中,您需要在其中初始化Firebase应用程序.

  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.

在您的 main 函数顶部调用 setupFirebaseAuthMocks(); 进行所有测试.

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

在您的 setUpAll 函数中,调用 await Firebase.initializeApp(); (您也可以将其放在 main 函数中在 setupFirebaseAuthMocks(); 下仍然可以使用).

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

现在您应该有一个模拟的Firebase应用程序.

Now you should have a mocked Firebase app.

这是一个完整的例子:

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
  });
}

这篇关于如何在扑朔迷离中模拟FirebaseApp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆