gpt4 book ai didi

flutter - 在mapEventToState中使用yield和Either时,测试我的bloc失败

转载 作者:行者123 更新时间:2023-12-03 03:03:40 28 4
gpt4 key购买 nike

我正在学习有关干净架构的类(class),并且遇到以下错误。
以下测试失败。

test('should emit [Error] when the input is invalid', () async {
final tNumberString = '1';
when(mockInputConverter.stringToUnsignedInteger(any))
.thenReturn(Left(InvalidInputFailure()));

final expected = [
Empty(),
Error(message: invalidInput),
];
expectLater(bloc.state, emitsInOrder(expected));

bloc.add(GetTriviaForConcreteNumberEvent(tNumberString));
});
我的NumberTriviaBloc如下
part 'number_trivia_event.dart';
part 'number_trivia_state.dart';

const String serverFailureMessage = 'Server Failure';
const String cacheFailureMessage = 'Cache Failure';
const String invalidInput =
'Invalid input - the number should be a positive integer';

class NumberTriviaBloc extends Bloc<NumberTriviaEvent, NumberTriviaState> {
NumberTriviaBloc(
{@required GetConcreteNumberTrivia concrete,
@required GetRandomNumberTrivia random,
@required this.inputConverter})
: assert(concrete != null),
assert(random != null),
assert(inputConverter != null),
_getConcreteNumberTrivia = concrete,
_getRandomNumberTrivia = random,
super(Empty());

final GetConcreteNumberTrivia _getConcreteNumberTrivia;
final GetRandomNumberTrivia _getRandomNumberTrivia;
final InputConverter inputConverter;

@override
Stream<NumberTriviaState> mapEventToState(
NumberTriviaEvent event,
) async* {
if (event is GetTriviaForConcreteNumberEvent) {
final inputEither =
inputConverter.stringToUnsignedInteger(event.numberString);

yield* inputEither.fold(
(l) async* {
yield Error(message: invalidInput);
},
(r) => throw UnimplementedError(),
);
}
}
}
我不太熟悉集团模式以及流和状态的测试机制。我猜测以某种方式无法正确执行代码,因为失败消息如下
ERROR: Expected: should do the following in order:
emit an event that Empty:<Empty>
emit an event that Error:<Error>
Actual: Empty:<Empty>
Which: was not a Stream or a StreamQueue

最佳答案

所以我自己解决了。问题中的代码对旧版本的dart有效。现在

bloc.state
将破坏代码。代替
bloc
应该用来获取状态。
第二个问题是,不再有可能
throw UnimplementedError();
在Either对象的fold方法中。无论哪种情况都将引发异常。 fold调用的正确功能中必须包含一些非异常逻辑。
修改反射(reflect)在下面的代码中。
number_trivia_bloc_test.dart
test('should emit [Error] when the input is invalid', () async {
when(mockInputConverter.stringToUnsignedInteger(any))
.thenReturn(Left(InvalidInputFailure()));

bloc.add(GetTriviaForConcreteNumberEvent(tNumberString));

final expected = [
Error(message: invalidInput),
];
expectLater(bloc, emitsInOrder(expected));
});
number_trivia_bloc.dart
@override
Stream<NumberTriviaState> mapEventToState(NumberTriviaEvent event,
) async* {
if (event is GetTriviaForConcreteNumberEvent) {
final inputEither =
inputConverter.stringToUnsignedInteger(event.numberString);

yield* inputEither.fold(
(failure) async* {
yield Error(message: invalidInput);
},
(integer) async* {
yield null;
},
);
}
}

关于flutter - 在mapEventToState中使用yield和Either时,测试我的bloc失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63562053/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com