Error when trying to test an async Dart ajax HttpR

2019-07-14 05:07发布

This question is a simplified version of this one: Testing dart ajax HttpRequest

I basically removed all the unnecessary code leaving only the test and HttpRequest.postFormData call.

The problem: test doesn't seem to wait until the Future completes. The test code:

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

}

As per advice, I return f and the test is supposed to wait until the Future completes. However, this is the output I'm getting instead:

~/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>

I think I clearly am misunderstanding something fundamental. I've tried multiple variations of this test, with async/await, expectAsync, completion, but nothing seems to be working. Would appreciate any good advice.

1条回答
淡お忘
2楼-- · 2019-07-14 05:44

I think what you want is this:

  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);
  });
查看更多
登录 后发表回答