2016-04-29 12 views
1

私はHttpRequest投稿をテストしようとしているときに何が起こっているのかよく分かりません。ここで仕事をして私のクラスのコードは次のとおりです。ダーツajaxをテストする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にあり、私はそれを私のサーバのログに見ることができます。

ただし、!!!!!!!!は印刷されません。 はまた、テストがで失敗します。私は、応答を送信する前に1秒間スリープ状態に自分のサーバーを強制した場合

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

を完了するので、テストを待つことができそうでない場合は、テストが到着したサーバからの応答が完了する前に返却する必要があります。

+0

しかし、 'HttpRequest.postFormData'は既にFutureを返しています。これが私の' post() 'メソッドから返ってきます。 https://api.dartlang.org/1.14.2/dart-html/HttpRequest/postFormData.html – snitko

+0

ああ、私は、テスト自体から戻ってきて、それを得ました。私はちょうどそれを試してみましたが、奇妙な出力でエラーが発生しました: '[object XMLHttpRequestProgressEvent]' – snitko

+0

'event.target.status'と' event.target.statusText'を ' 'XMLHttpRequestProgressEvent' –

関連する問題