我正在尝试在我的服务中测试流事件,但遇到了一个问题:有什么方法可以在异步流中同步传送事件?这是简化的代码示例:
test("deliver event", () {
StreamController sc = new StreamController();
String v = "old";
sc.stream.listen((val) {v = val;});
sc.add("new");
expect(v, "new"); // test fails: actual value is "old"
});
最佳答案
你不能从异步到同步。
import 'dart:async';
Future main() async {
StreamController sc = new StreamController();
String v = "old";
var subscr = sc.stream.listen((val) { v = val;});
sc.add("new");
subscr.asFuture().then((_) {
print('assert v == "new": ${v == 'new'}');
// expect(v, "new"); // test fails: actual value is "old"
});
sc.close();
}
关于testing - 强制流事件传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38916001/