【问题标题】:Error when trying to test an async Dart ajax HttpRequest尝试测试异步 Dart ajax HttpRequest 时出错
【发布时间】:2016-08-25 14:01:29
【问题描述】:

这个问题是这个问题的简化版:Testing dart ajax HttpRequest

我基本上删除了所有不必要的代码,只留下了测试和HttpRequest.postFormData 调用。

问题:测试似乎没有等到 Future 完成。 测试代码:

# http_report_adapter_test.dart

import "package:test/test.dart";
import 'dart:html';

void main() {

  test("sends an ajax request and acknowledges a 200 response from the server", () {
    var f = HttpRequest.postFormData("http://localhost:4567/errors", { 'hello': 'world'});
    f.then((_) => print("!!!!!!!"));
    return f;
  });

}

根据建议,我 return f 并且测试应该等到 Future 完成。但是,这是我得到的输出:

~/Work/my_libs/dart/logmaster master$ dtest-d -n "sends an ajax request and acknowledges a 200 response from the server"
00:05 +0 -1: test/http_report_adapter_test.dart: sends an ajax request and acknowledges a 200 response from the server
  [object XMLHttpRequestProgressEvent]
  dart:html                            HttpRequest.postFormData
  http_report_adapter_test.dart 14:25  main.<fn>

  [object XMLHttpRequestProgressEvent]
  dart:html                            HttpRequest.postFormData
  http_report_adapter_test.dart 14:25  main.<fn>

我想我显然误解了一些基本的东西。我已经尝试了这个测试的多种变体,async/awaitexpectAsynccompletion,但似乎没有任何效果。希望有任何好的建议。

【问题讨论】:

    标签: ajax dart async-await dart-async


    【解决方案1】:

    我想你想要的是这样的:

      test("sends an ajax request and acknowledges a 200 response from the server", () async {
        await HttpRequest.postFormData("http://localhost:4567/errors", { 'hello': 'world'});
    
        print("hello!"); // or expect(x, y);
      });
    

    【讨论】:

    • 我已经按照您的建议进行了确切的更改,输出如下:pastie.org/10818961
    • 看起来你的测试失败了。不幸的是,Http 错误并没有多大帮助。
    • 是的,但为什么会失败?我的意思是,只有一行使用了 Dart 的 std lib 类 - HttpRequest 。它尝试连接的服务器清楚地接收到请求(我可以在日志中看到它)。这里实际上有 0 个我自己的代码。
    猜你喜欢
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    相关资源
    最近更新 更多