weixin_33697898 2016-04-29 19:07 采纳率: 0%
浏览 57

测试dart ajax 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);

  }

}

An here's the test code:

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("!!!!!!!!!"));
  });

}

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.

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.

  • 写回答

1条回答 默认 最新

  • weixin_33688840 2016-04-29 19:11
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度