测试dart ajax HttpRequest [英] Testing dart ajax HttpRequest

查看:148
本文介绍了测试dart ajax HttpRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太确定我明白当我尝试测试后HttpRequest发生了什么。这是我的类的代码,执行的工作:

I'm not quite sure I understand what's going on when I try testing post HttpRequest. Here's the code of my class that does the job:

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 ,我可以看到它在我的服务器的日志。

For now, as you can see, I'm not even trying to test anything, just output something. While running this, the request does indeed to to http://localhost:4567/errors and I can see it in my server's logs.

但是,不打印 !!!!!!!!
此外,测试失败:

However, the !!!!!!!! isn't printed. Additionally, the test fails with:

This test failed after it had already completed. Make sure to use 
[expectAsync] or the [completes] matcher when testing async code.

但是,如果我强制我的服务器睡眠1秒钟然后发送响应,一个错误。

However, if I force my server to sleep for 1 second before sending a response, the test passes without an error.

如果有人会帮助我做一些感觉,因此,写正确的测试,我会感激。

I would appreciate if someone would help me make some sense of it all, and, consequently, write the correct test.

推荐答案

您需要返回未来,以便测试可以等待其完成

You need to return the Future so the test can wait for it to complete

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

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

Otherwise the test completes before the response from the server arrives.

这篇关于测试dart ajax HttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆