gpt4 book ai didi

ajax - 测试dart ajax HttpRequest

转载 作者:行者123 更新时间:2023-12-03 03:43:39 25 4
gpt4 key购买 nike

我不确定我尝试测试HttpRequest后会发生什么。这是完成任务的我类的代码:

import 'dart:html';

class HttpReportAdapter {

var logmaster;
int log_level = 2;
String url;

HttpReportAdapter(this.url) {}

post(r, level) {

var data = {
'message' : r,
'log_level' : log_level.toString(),
};

if(this.logmaster != null)
data['log_level_string'] = this.logmaster.log_level_as_string(level);

return HttpRequest.postFormData(this.url, data);

}

}

这是测试代码:
import '../lib/report_adapters/http_report_adapter.dart';
import "package:test/test.dart";

void main() {

var adapter;

setUp(() {
adapter = new HttpReportAdapter("http://localhost:4567/errors");
});

test("sends an ajax request and acknowledges a 200 response from the server", () {
adapter.post("message", 2).then((_) => print("!!!!!!!!!"));
});

}

如您所见,目前,我什至没有尝试测试任何东西,只是输出一些东西。在运行此命令时,请求确实针对 http://localhost:4567/errors,我可以在服务器日志中看到它。

但是,不会打印 !!!!!!!!
此外,测试失败并显示:
This test failed after it had already completed. Make sure to use 
[expectAsync] or the [completes] matcher when testing async code.

但是,如果我在发送响应之前强制服务器休眠1秒钟,则测试通过且没有错误。

如果有人能帮助我弄清这一切,并因此编写正确的测试,我将不胜感激。

最佳答案

您需要返回Future,以便测试可以等待它完成

return adapter.post("message", 2).then((_) => print("!!!!!!!!!"));

否则,测试将在服务器的响应到达之前完成。

关于ajax - 测试dart ajax HttpRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36945795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com