0

AjaxRequest クラスに関するテスト スイートを作成しようとしていますが、リクエストの本文を調べようとすると、このテスト エラーが発生します。

FAILED TESTS:
AjaxRequest
  #POST
  ✖ attaches the body to the response
    PhantomJS 1.9.8 (Mac OS X 0.0.0)
  Expected Object({ example: [ 'text' ] }) to equal Object({ example: 'text' }).

単体テストの関連部分は次のとおりです。

      req = new AjaxRequest().post('http://example.com')
            .body({
                example: 'text'
            }).run();

そしてrun()、これがajaxリクエストが行われるメソッドです

var options = {
        url: this._url,
        method: this._method,
        type: 'json',
        data: this._body
    };

    return when(reqwest(options));

reqwestを使用して ajax リクエストを発行しています。

['text']リクエスト'text'がjson本体で送信されたときに期待している理由を誰かが指摘できますか?

ありがとうございました!

4

1 に答える 1

0

AjaxRequest の実装を変更すると、問題が解決しました。

runこれが使用の新しい実装ですXMLHttpRequest

run () {
    var req = new XMLHttpRequest();

    req.open(this._method, this._url, true);

    req.send(JSON.stringify(this._body));

    return when.promise((resolve, reject) => {
        req.onload = function() {
            if (req.status < 400) {
                var param = req.response;
                try { param = JSON.parse(param) } catch (e) { };
                resolve(param);
            } else {
                reject(new RequestError(req.statusText, req.status));
            }
        };
    });
}

これにより、余分なライブラリがなくなるだけでなく、リクエストの promise をいつ拒否するかをより細かく制御できます。

于 2015-08-12T12:35:40.007 に答える