Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Testing 强制流事件交付_Testing_Dart - Fatal编程技术网

Testing 强制流事件交付

Testing 强制流事件交付,testing,dart,Testing,Dart,我试图在我的服务中测试流事件,但遇到了一个问题:有没有办法在异步流中同步传递事件?以下是简化的代码示例: 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

我试图在我的服务中测试流事件,但遇到了一个问题:有没有办法在异步流中同步传递事件?以下是简化的代码示例:

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();
}

我更新了我的答案。