如何在Dart中测试流 [英] How to test a stream in Dart
问题描述
如何在Dart中测试流?我有以下代码:
test('单词顺序读取正确',(){WordTrackerInterface wordTracker = WordTracker.byContent('0 1 2');wordTracker.setWordsCountPerChunk(1);var stream = wordTracker.nextWordStringStream();预计(溪流,emitsInOrder(List< Word> .generate(6,(i)=>我>2?Word(''):Word(i.toString()))));对于(int i = 0; i< 6; i ++){wordTracker.nextWord();}});
我需要测试作为 String
的成员数据 Word :: content
是否等于 emitsInOrder
中提供的内容./p>
类似于以下内容的流
: expect(溪流,emitsInOrder(List< Word> .generate(6,(i)=>我>2?Word(''):Word(i.toString())),期望((实际单词数,期望单词数){返回actual.content ==预期内容;}));
阅读dart文件中的源代码并在Internet上阅读后,我找到了解决方案:我需要创建一个自定义Matcher.我在笔记本电脑上测试了以下代码,并通过引用应用程序中的其他文件(如"WordTracker"),代码按预期运行.
test('单词顺序读取正确',(){WordTrackerInterface wordTracker = WordTracker.byContent('0 1 2');wordTracker.setWordsCountPerChunk(1);var stream = wordTracker.nextWordStringStream();期望(流,emitsInOrder(List< Word> .generate(6,(i)=> i> 2?Word(''):Word(i.toString())).map< WordMatcher>((字值)=>WordMatcher(value))));for(int i = 0; i< 6; i ++){wordTracker.nextWord();}});WordMatcher类扩展Matcher {预期字词;实际字词;WordMatcher(this.expected);@override说明describe(描述说明){返回description.add(期望的单词内容='$ {expected.content}'"));}@override说明describeMismatch(动态项目,说明不匹配说明,映射<动态,动态>matchState,布尔冗长){return mismatchDescription.add(具有实际发出的单词内容='$ {matchState ['actual'].content}''");}@override布尔匹配(实际,地图matchState){this.actual =实际;matchState ['actual'] =实际是Word吗?实际的:Word('未知');return(实际为Word).content == Expected.content;}}
How to test a stream in dart? I have this code:
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();
}
});
I need to test that the member data Word::content
which is a String
is equal to that provided in the emitsInOrder
.
Something like the following for 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;
}));
After reading the source code in dart files and reading on the internet, I found the solution: I needed to create a custom Matcher. I tested the following code on my laptop and by referencing other files from my application like 'WordTracker', the code ran as expected.
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中测试流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!