0

http呼び出しをインターセプトするためにnockを使い始めました。以下は、必要なモジュールをインポートした後、単体テスト ケースで使用しているコードです。

it('testing superagent using nock..', function () {

    let mockData = {
        scope: this,
        data: {"title":"test"},
        callback: function (data){
            //expecting {id:10} here.but nothing is coming
            console.log(data)
        },
        errback: function(err) {console.log(err)}
    };

    nock('http://localhost').post('/api/1234').reply(200, {id: 10});

    callFunction(actionFunction, mockData, this);
}

callFunction は、パラメーター「actionFunction」、「mockData」を別のクラスに送信します。このクラスでは、actionFucntion が mockData を受け取り、スーパーエージェント モジュールを使用して ajax リクエストを作成します。

import xhr from './xhr';
static  actionFunction(mockData){
let xhr = mockData.xhr || xhr;
xhr.reqXhr({
method: :"POST",
url: mockData.url,
data: mockData.data,
callback: function(data) {mockData.callback.call(mockData.scope, data);},
errback: function(response) {mockData.errback.call(mockData.scope, response);}
})
}

//in xhr class I am triggering the actual ajax call using super agent

super-agent(method, url).send(reqdata).end((errror, response) => {
    //sending response.body to call backs similar to below
    request.callback.call(this, response.body);
    }

問題は、nock が http 呼び出しをインターセプトできたにもかかわらず、nock モック応答 ({id:10}) がコールバック関数に到達していないことです。このレスポンスをコールバック関数で表示したい

これを行う方法はありますか?

4

0 に答える 0