簡體   English   中英

Dart 單元測試框架的 `expect` 似乎無法正常工作

[英]Dart unit test framework's `expect` does not seem to work properly

這是一個對象的 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([], []); 作品。 那對我來說沒有任何意義。

測試失敗,因為[] == []在 Dart 中為false 您可以使用collection 包來處理集合相等性。

就我而言, Equitable有所幫助。 應用於 Andrey 的案例 MapState 類應該擴展了 Equatable。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM