Unit testing Dart单元测试框架&x27;s'expect'似乎不能正常工作
下面是要比较的对象deUnit testing Dart单元测试框架&x27;s'expect'似乎不能正常工作,unit-testing,dart,Unit Testing,Dart,下面是要比较的对象deMapState: class MapState { final Coordinate currentCoordinate; final Iterable<Coordinate> markers; MapState(this.currentCoordinate, this.markers); MapState.empty() : this.currentCoordinate = null, this.markers
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;
}
结果(当然是失败):
应为“MapState”的实例
实际值:“MapState”的实例
包:test_api expect
包装:颤振测试/src/widget测试仪。dart 196:3预期
测试/应用程序/映射块测试。dart 25:5主
我将原始测试简化为此,以跟踪错误,因此不要被单元测试的无意义所迷惑
更改expect(MapState.empty(),MapState.empty())代码>到期望值(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';
注:奇怪的是,将this.markers=[]
更改为this.markers=const[]
会有所帮助。胡特???无论如何,expect([],[])代码>工作。这对我来说毫无意义。测试失败,因为Dart中的[]=[]
为false
。您可以使用来处理集合相等性。在我的例子中有帮助。应用于Andrey的case MapState类应该扩展了Equalable。这完全是胡说八道。为什么expect([],[])代码>通过和期望(true,[]=[])代码>没有?x、 xBecauseexpect
处理集合并逐元素比较它们。在expect
方法的文档中甚至没有提到的东西。。。谢谢你的帮助@安德烈·伊利尤宁(AndreyIlyunin)记录在案,尽管是间接的,也不是最清楚的。状态:“matcher
可以是一个值,在这种情况下,它将被包装在一个matcher中。”等于matcher的文档中说:“对于Iterables
和映射
,这将递归地匹配元素。”
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';