这是比较MapState
的对象的定义:
class MapState {
final Coordinate currentCoordinate;
final Iterable<Coordinate> markers;
MapState(this.currentCoordinate, this.markers);
MapState.empty()
: this.currentCoordinate = null,
this.markers = [];
MapState.withCoordinate(this.currentCoordinate) : this.markers = [];
MapState.withMarkers(this.markers) : this.currentCoordinate = null;
MapState newCoordinate(final Coordinate coordinate) =>
MapState(coordinate, this.markers);
MapState newMarkersSet(final Iterable<Coordinate> markers) =>
MapState(this.currentCoordinate, markers);
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is MapState &&
runtimeType == other.runtimeType &&
currentCoordinate == other.currentCoordinate &&
markers == other.markers;
@override
int get hashCode =>
currentCoordinate.hashCode ^
markers.hashCode;
}
这是单元测试:
test('init, Observer should receive an empty state as a first state',
() {
expect(MapState.empty(), MapState.empty());
});
结果(当然会失败):
预期:“ MapState”的实例
实际:“ MapState”的实例
package:test_api期望
package:flutter_test / src / widget_tester.dart 196:3期望
test / application / map_bloc_test.dart 25:5 main。
我将原始测试简化为该错误以跟踪错误,因此不要被毫无意义的单元测试所迷惑。
将expect(MapState.empty(), MapState.empty());
更改为expect(1, 1);
会有所帮助,但是我无法使其与我的对象一起使用。
此外,这是我的导入块:
import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:taxi/application/map_bloc.dart';
import 'package:taxi/domain/map.dart';
P.S .:很奇怪,但是将this.markers = []
更改为this.markers = const []
会有所帮助。啊?无论如何,expect([], []);
可以工作。这对我来说毫无意义。
答案 0 :(得分:1)
测试失败,因为Dart中[] == []
是false
。您可以使用collection package来处理集合相等性。
答案 1 :(得分:0)
在我看来,Equitable有所帮助。适用于Andrey的案例MapState类应具有扩展的Equatable。