我近 20 天正在学习 Flutter Clean Architecture TDD 课程。我正在编写 Stream (mapEventToState) 的单元测试,这是我的测试代码:
\ntest(\n 'should emit [Error] when the input is invalid',\n () async {\n // arrange\n when(mockInputConverter!.stringToUnsignedInteger(SIGNED_STRINGS))\n .thenReturn(Left(InvalidInputFailure()));\n // assert later\n final expected = [\n // The initial state is always emitted first\n NumberTriviaEmpty(),\n NumberTriviaError(message: INVALID_INPUT_FAILURE_MESSAGE),\n ];\n expectLater(bloc!.state, emitsInOrder(expected));\n // act\n bloc!.add(GetTriviaForConcreteNumber(tNumberString));\n },\n);\nRun Code Online (Sandbox Code Playgroud)\n忘记所有关于(何时)的事情,因为我已经模拟了所有类并简单地返回了一个 Left of Either ,这显然是一个失败,这很好。因此,预期数据的问题出现了,我只是创建了预期列表,并将其按顺序“emitsInOrder”传递到“expectLater”方法中。
\n我的 Stream 实现代码在这里:
\n@override\nStream<NumberTriviaState> mapEventToState(NumberTriviaEvent event,) async* {\nif (event is GetTriviaForConcreteNumber) {\n final inputEither =\n inputConverter.stringToUnsignedInteger(event.numberString);\n\n yield* inputEither!.fold(\n (failure) async* {\n yield NumberTriviaError(message: INVALID_INPUT_FAILURE_MESSAGE);\n },\n // Although the "success case" doesn't interest us with the current test,\n // we still have to handle it somehow.\n (integer) => throw UnimplementedError(),\n );\n}\nRun Code Online (Sandbox Code Playgroud)\n我在标题中放入的[错误]未完成,因为它有点长,我在这里分享它,请看一下我实际上做错了什么。
\nExpected: should do the following in order:\n \xe2\x80\xa2 emit an event that NumberTriviaEmpty:<NumberTriviaEmpty()>\n \xe2\x80\xa2 emit an event that NumberTriviaError:<NumberTriviaError(Invalid Input - The number must be a positive integer or zero.)>\n Actual: NumberTriviaEmpty:<NumberTriviaEmpty()>\n Which: was not a Stream or a StreamQueue\nRun Code Online (Sandbox Code Playgroud)\n我已经做了所有这些相同的事情,它对我的期望是什么,但它仍然抛出这个异常。我陷入这个错误大约 4 天,我到处搜索,但没有找到任何修复,如果我做错了什么,请在评论部分或答案中提出您的建议。谢谢 :)
\n小智 9
除了将 'bloc!.state' 更改为 'bloc'(根据 QuangNV 和 ouflak)之外,还尝试将测试中的 'async' 更改为异步生成器'async*',以匹配 Stream 中使用的代码执行。
test(
'should emit [Error] when the input is invalid',
() async* {
// arrange
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2058 次 |
| 最近记录: |