这个问题是这个问题的简化版本:Testing dart ajax HttpRequest
我基本上删除了所有不必要的代码,仅保留了test和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/await
,expectAsync
,completion
进行此测试的多种变体,但是似乎没有任何效果。将不胜感激任何好的建议。
最佳答案
我认为您想要的是:
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);
});
关于ajax - 尝试测试异步Dart Ajax HttpRequest时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36954761/