unit-testing - Dart 单元测试框架的 `expect` 似乎无法正常工作
问题描述
这是要比较的对象的 de 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”的实例
包:test_api 期望
包:flutter_test/src/widget_tester.dart 196:3 期望
测试/应用程序/map_bloc_test.dart 25:5 主要。
我将原始测试简化为跟踪错误,所以不要对单元测试的无意义感到困惑。
更改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';
PS:奇怪的是,但更改this.markers = []
为有this.markers = const []
帮助。呜???无论如何,expect([], []);
工作。这对我来说没有任何意义。
解决方案
测试失败是因为[] == []
在false
Dart 中。您可以使用集合包来处理集合相等性。
推荐阅读
- ember.js - 如何在 Ember Octane 中通过 javascript 将子组件添加到主组件
- android-studio - Android Studio 第二个窗口打开
- function - 如何在golang中返回一个空值?
- laravel - Laravel Cashier 在发票/收据上自动发送电子邮件
- php - ldap_search():返回的部分搜索结果:超出 Sizelimit 只检索 1000 个用户
- java - Android Studio:使用 mediaPlayer 启动和停止音频
- javascript - 将输入文本转换为按钮
- java - 在 Spring Boot 中无法检测到存储库注释
- javascript - Firebase 身份验证用户界面 - 显示注册
- python - 井字游戏蟒蛇