如何测试dart 中的水流?我有这个代码:

test('words are reading sequentially correct', () {
  WordTrackerInterface wordTracker = WordTracker.byContent('0 1 2');
  wordTracker.setWordsCountPerChunk(1);
  var stream = wordTracker.nextWordStringStream();

  expect(
      stream,
      emitsInOrder(List<Word>.generate(
          6, (i) => i > 2 ? Word('') : Word(i.toString()))));

  for (int i = 0; i < 6; i++) {
    wordTracker.nextWord();
  }
});

我需要测试成员数据Word::content(String)是否等于emitsInOrder中提供的数据.

STREAM类似于以下内容:

expect(
    stream,
    emitsInOrder(List<Word>.generate(
        6, (i) => i > 2 ? Word('') : Word(i.toString()))),
    expect((Word actual, Word expected) {
  return actual.content == expected.content;
}));

推荐答案

在阅读了DART文件中的源代码并在互联网上阅读之后,我找到了解决方案:我需要创建一个自定义的Matcher.我在笔记本电脑上测试了以下代码,通过引用应用程序中的其他文件(如"WordTracker"),代码按预期运行.

test('words are reading sequentially correct', () {
    WordTrackerInterface wordTracker = WordTracker.byContent('0 1 2');
    wordTracker.setWordsCountPerChunk(1);
    var stream = wordTracker.nextWordStringStream();

    expect(stream, 
      emitsInOrder(List<Word>.generate(6, (i) => i > 2 ? Word('') : Word(i.toString())).map<WordMatcher>(
        (Word value) => WordMatcher(value))));

    for (int i = 0; i < 6; i++) {
      wordTracker.nextWord();
    }
  });


class WordMatcher extends Matcher {
  Word expected;
  Word actual;
  WordMatcher(this.expected);

  @override
  Description describe(Description description) {
    return description.add("has expected word content = '${expected.content}'");
  }

  @override
  Description describeMismatch(
    dynamic item,
    Description mismatchDescription,
    Map<dynamic, dynamic> matchState,
    bool verbose
  ) {
    return mismatchDescription.add("has actual emitted word content = '${matchState['actual'].content}'");
  }

  @override
  bool matches(actual, Map matchState) {
    this.actual = actual;
    matchState['actual'] = actual is Word ? actual : Word('unknown');
    return (actual as Word).content == expected.content;
  }
}

Dart相关问答推荐

有没有办法否定IF条件中的模式大小写匹配?

对字母数字字符串数组进行排序

是否从 Dart 中删除了interface关键字?

在 Flutter 中将 Widget 放在 ListView 之上

错误:不要从另一个包中导入实现文件

在 Dart 中,List.unmodifiable() 和 UnmodifiableListView 有什么不同?

Dart / Flutter 错误:没有为类Logger定义toStringDeep

在 Windows 10 中使用 Android Studio 时没有Remove Widget选项

使用 Google Dart 进行数据库查询?

判断无状态小部件是否在Flatter中处理

无法导入 dart 的 intl 包

常数在定义Flutter边缘集中的作用

如何更改Flatter DevTools的默认浏览器?

如何突出显示所选卡片的边框?

如何在 Dart 中上传文件?

如何使用 Dart 和 web 以 60fps 的速度驱动动画循环?

Dart:你如何让 Future 等待 Stream?

你如何在 Dart 中打印美元符号 $

Dart 会支持使用现有的 JavaScript 库吗?

如何在 Dart 中生成随机数?