Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 如何在Flutter中为转换流编写测试?_Firebase_Flutter_Dart_Stream - Fatal编程技术网

Firebase 如何在Flutter中为转换流编写测试?

Firebase 如何在Flutter中为转换流编写测试?,firebase,flutter,dart,stream,Firebase,Flutter,Dart,Stream,我有一个提供者,它有一个方法,将Firebase中的数据作为流,将其转换为列表并返回流。我正在尝试编写一个测试,我想检查列表中的项目是否与我期望的相同。我该怎么做 我当前的代码: test('getContacts returns a empty list when there is no contact',() async{ when(sharedPreferencesMock.get(any)).thenReturn('uid'); //mock the sharedprefs

我有一个提供者,它有一个方法,将Firebase中的数据作为流,将其转换为列表并返回流。我正在尝试编写一个测试,我想检查列表中的项目是否与我期望的相同。我该怎么做

我当前的代码:

test('getContacts returns a empty list when there is no contact',() async{
      when(sharedPreferencesMock.get(any)).thenReturn('uid'); //mock the sharedprefs
      documentSnapshot = DocumentSnapshotMock();  //mock documentsnapshot
      when(documentSnapshot.exists).thenReturn(true); // this is done to pass the getUidByUsername method
      documentReference = DocumentReferenceMock(documentSnapshotMock: documentSnapshot);
      documentReference.setData({
        'uid':'uid',
        'contacts':[]  // setting the usename in the data already so that duplicate contact exception is thrown
      });
      userDataProvider.getContacts().asBroadcastStream().listen((data){
        expect(data.length,0);
      });

    });
和提供者方法

  @override
  Stream<List<Contact>> getContacts() {
    CollectionReference userRef = fireStoreDb.collection(Paths.usersPath);
    DocumentReference ref =
        userRef.document(SharedObjects.prefs.get(Constants.sessionUid));


    return ref.snapshots().transform(StreamTransformer<DocumentSnapshot, List<Contact>>.fromHandlers(handleData: (documentSnapshot, sink) async{
      List<String> contacts;
      if (documentSnapshot.data['contacts'] == null) {
        ref.updateData({'contacts': []});
        contacts = List();
      } else {
        contacts = List.from(documentSnapshot.data['contacts']);
      }
      List<Contact> contactList = List();
      for (String username in contacts) {
        print(username);
        String uid = await getUidByUsername(username);
        DocumentSnapshot contactSnapshot = await userRef.document(uid).get();
        contactList.add(Contact.fromFirestore(contactSnapshot));
      }
      sink.add(contactList);
    }));

  }
更新:


将当前传递给StreamTansformer的lambda函数设置为单独的函数并进行测试。
如果您想测试完整的函数,pub上有一个Firebase模拟包

非常感谢!我提取了lambda函数并为其编写了测试。它正在过去。我已经用代码片段更新了这个问题。你能检查一下这样做是否正确吗?好的。谢谢@Thomas
 StreamController streamController = StreamController<List<Contact>>();
      StreamSink<List<Contact>> sink = streamController.sink;
      Stream<List<Contact>> stream = streamController.stream;
      stream.listen((List<Contact> list){
        expect(list.length,1);
      });
      userDataProvider.mapDocumentToContact(userCollection, userRef, documentSnapshot, sink);
      streamController.close();