gpt4 book ai didi

firebase - 如何使用 Firebase Cloud Firestore 对方法进行单元测试?

转载 作者:IT王子 更新时间:2023-10-29 06:46:00 25 4
gpt4 key购买 nike

我正在尝试在 Flutter 上使用 Cloud Firestore。到目前为止,我已经能够使用它,但我想在我的项目中包含使用 firestore 的函数的单元测试。我正在使用 Mockito 做同样的事情,我认为它应该有效。

但我真的对 Dart 的 StreamMatchers 感到困惑。我无法理解错误消息。

下面是我试图实现的一些代码以及我遇到的错误。

repository.dart

    import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:sunapsis_conference18/models/conference_event.dart';

Stream<List<ConferenceEvent>> getAllEvents() {
return _firestore
.collection(_collectionName)
.snapshots()
.map((QuerySnapshot snapshot) => _eventMapper(snapshot));
}


List<ConferenceEvent> _eventMapper(QuerySnapshot snapshot) {
List<ConferenceEvent> events = [];
for (int i = 0; i < snapshot.documents.length; i++) {
DocumentSnapshot documentSnapshot = snapshot.documents[i];
ConferenceEvent event =
ConferenceEvent.buildFromMap(documentSnapshot.data);
events.add(event);
}
return events;
}

It 方法尝试从 firestore 集合中获取数据并返回数据对象列表流。

repository_test.dart

    import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:mockito/mockito.dart';
import 'package:sunapsis_conference18/models/conference_event.dart';
import 'package:sunapsis_conference18/repository/repository.dart'; //quotation mark fix
import 'package:test/test.dart';

class MockDocumentReference extends Mock implements DocumentReference {}

class MockFirestore extends Mock implements Firestore {}

class MockCollectionReference extends Mock implements CollectionReference {}

class MockQuerySnapshot extends Mock implements QuerySnapshot {}

class MockDocumentSnapshot extends Mock implements DocumentSnapshot {}

class MockQuery extends Mock implements Query {}

main() {
group('getAllEvents() tests', () {
final Firestore mockFirestore = MockFirestore();
final CollectionReference mockCollectionReference =
MockCollectionReference();
final QuerySnapshot mockQuerySnapshot = MockQuerySnapshot();
final DocumentSnapshot mockDocumentSnapshot = MockDocumentSnapshot();
final Repository repository = Repository(mockFirestore);
final DocumentReference _mockDocumentRef = MockDocumentReference();
final Map<String, dynamic> _responseMap = {
'foo': 123,
'bar': 'Test title',
};
final ConferenceEvent _event = ConferenceEvent.buildFromMap(_responseMap);

test('returns correct stream of list of ConferenceEvent', () async {
when(mockFirestore.collection('events'))
.thenReturn(mockCollectionReference);
when(mockCollectionReference.snapshots())
.thenAnswer((_) => Stream.fromIterable([mockQuerySnapshot]));

when(mockQuerySnapshot.documents).thenReturn([mockDocumentSnapshot]);
when(mockDocumentSnapshot.data).thenReturn(_responseMap);
await expectLater(
repository.getAllEvents(),
emitsAnyOf([
[_event],
emitsDone
]));
});
});
}

我无法制定正确的流匹配器,同时也无法理解错误消息。

错误

Expected: should do one of the following:
• emit an event that [Instance of 'ConferenceEvent']
• be done
Actual: <Instance of '_MapStream<QuerySnapshot, List<ConferenceEvent>>'>
Which: emitted • [Instance of 'ConferenceEvent']
x Stream closed.
which failed all options:
• failed to emit an event that [Instance of 'ConferenceEvent'] because it emitted an event that was <Instance of 'ConferenceEvent'> instead of <Instance of 'ConferenceEvent'> at location [0]
• failed to be done

任何有助于理解错误消息的指导将不胜感激。另外,这是测试此功能的正确方法吗?

最佳答案

我认为问题与方式有关expectLater正在比较 ConferenceEvent 的实例。它正在使用 == (等于)运算符来比较这两个对象,并且在不覆盖 ConferenceEvent 中的等于运算符的情况下,这些对象不会具有相同的 hashCode .

在你模拟的代码中有一个新对象在这里实例化

ConferenceEvent event =
ConferenceEvent.buildFromMap(documentSnapshot.data);

稍后您将它与在此行中创建的 ConferenceEvent 的另一个对象进行比较:

final ConferenceEvent _event = ConferenceEvent.buildFromMap(_responseMap);

关于firebase - 如何使用 Firebase Cloud Firestore 对方法进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52268590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com