[英]Flutter: BLoC, testing streams
Testing the bloc pattern is not so clear to me. 对我来说,测试集团模式还不是很清楚。 So, if I have these 2 stream controllers:
因此,如果我有以下两个流控制器:
final _controller1 = StreamController();
final _controller2 = StreamController<bool>;
Sink get controller1Add = _controller1.sink;
Stream<bool> get controller2Out = _controller2.stream;
and I want to test that, from this function: 我想通过此功能进行测试:
submit() {
if (_controller1.value == null ||
_controller1.value.isEmpty) {
print(...)
return;
}else
_controller2.sink.add(true);
}
the _controller2.stream should have true, how should I do? _controller2.stream应该为true,该怎么办?
I tried something like: 我尝试了类似的东西:
test("test", (){
bloc.submit();
expect(bloc.controller2Out, emitsAnyOf([true]));
});
but, of course, it didn´t work. 但是,当然,它没有用。
I've modified your code to use the RxDart's BehaviorSubject and it seems to work. 我已经修改了您的代码以使用RxDart的BehaviorSubject,它似乎可以正常工作。 You are using StreamController but I get error cause it doesn't have the value property.
您正在使用StreamController,但是由于没有value属性,因此出现错误。
final _controller1 = BehaviorSubject<String>();
final _controller2 = BehaviorSubject<bool>();
Sink get controller1Add => _controller1.sink;
Stream<bool> get controller2Out => _controller2.stream;
submit() {
if (_controller1.value == null || _controller1.value.isEmpty) {
print('Error');
_controller2.sink.add(false);
return;
} else {
print('OK');
_controller2.sink.add(true);
}
}
The test: 考试:
bloc.controller1Add.add('');
bloc.submit();
expect(bloc.controller2Out, emits(false));
bloc.controller1Add.add('test');
bloc.submit();
expect(bloc.controller2Out, emits(true));
bloc.controller1Add.add('');
bloc.submit();
expect(bloc.controller2Out, emits(false));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.