我是新开发人员,如果这是一个愚蠢的问题,我深表歉意。
我正在尝试在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;
}
该方法尝试从Firestore集合中获取数据并返回数据对象列表流。
repository_test.dart 导入'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;
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
我们将不胜感激任何帮助您理解错误消息的指南。另外,这是测试此功能的正确方法吗?
答案 0 :(得分:0)
我认为问题与expectLater比较ConferenceEvent
实例的方式有关。它使用==(等于)运算符比较这两个对象,而没有覆盖ConferenceEvent
中的equal运算符,这些对象将没有相同的hashCode。
在模拟代码中,有一个在此处实例化的新对象
ConferenceEvent event =
ConferenceEvent.buildFromMap(documentSnapshot.data);
然后将其与在此行中创建的另一个ConferenceEvent
对象进行比较:
final ConferenceEvent _event = ConferenceEvent.buildFromMap(_responseMap);