Flutter Firebase功能的单元测试

Fra*_*ank 8 dart google-cloud-firestore flutter-test

美好的一天,我正在尝试对以下在Cloud Firestore中创建文档的功能进行一些单元测试。我在我的应用程序中使用了一个函数,它创建了一个文档,但是我想编写一个test.dart文件,该文件对以下函数执行单元测试,甚至在控制台上打印一些输出以进行验证。

我认为我没有以正确的方式编写Test.dart。我得到一个错误。

文件createdatabase.dart中的功能

Future<dynamic> createDoc(dataMap,collection) async {
  final TransactionHandler createTransaction = (Transaction tx) async {
    final DocumentSnapshot ds = await tx.get(db.collection(collection).document());
    final Map<String, dynamic> result = {};
    result.addAll(dataMap);
    result['id'] = ds.documentID;
    await tx.set(ds.reference, result);

    return result;
  };
Run Code Online (Sandbox Code Playgroud)

测试飞镖

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:test/test.dart';
import '../lib/service/createfirebase.dart';

void main() {
  CreateFirebase cf = new CreateFirebase();   
   //test    
    test('Creating doc on firestore ', () async{ 
      Object dataObj ={'name':'Dev','title':'Dev'};
      var create = await cf.createDoc(dataObj, 'crude');
      expect(true,create);
      print('The doc details are');
      print(dataObj);
    });

}
Run Code Online (Sandbox Code Playgroud)

运行此测试后的错误是MissingPluginException(在渠道plugins.flutter.io/cloud_firestore上未找到方法Firestore#runTransaction的实现)

但是我不明白为什么,因为我拥有所有依赖关系,并且如果我在另一个类中调用该函数,则会创建文档。但是在此测试中调用会出现上述错误。我想我做的方式不正确。

我可以看的任何贡献或参考可以帮助测试此类功能?

nic*_*ssi 0

It's been a while since this question was submitted, but just to not let it with no reply.

As I stumbled in the same error I found this useful issue: https://github.com/flutter/flutter/issues/13971

Basicaly reinstall all the packages with:

flutter clean
flutter packages get
Run Code Online (Sandbox Code Playgroud)

Hope it helps.