使用nock时嵌套请求不成功

时间:2017-05-01 08:20:02

标签: javascript node.js unit-testing nock

我定义了以下简单函数,我想用nock测试它。

function checkJobAndUpload(ycgResponse, targetJob, integrationId, jobBoardId, callback) {
    const options = {
        uri: 'https://multiposting.stepstone.com/status/1',
        method: 'GET',
        resolveWithFullResponse: true
    };
    const payload = JSON.stringify({ message: 'success' })
    const put_options = {
        uri: 'https://multiposting.stepstone.com/listings/1',
        method: 'PUT',
        body: payload,
        resolveWithFullResponse: true
    };
    rp(options).then(response => {
        console.log('Status call succeeded')
        return rp(put_options)
    }).then(response => {
        console.log('Update call succeeded')
        callback(null, { statusCode: 200 });
    }).catch(error => {
        callback({ statusCode: 500 });
    });
}

以下是我的nock设置。

it('Should succeed in check, update and upload job status functions', () => {
    nock.cleanAll();
    var statusAPI = nock('https://multiposting.stepstone.com').get(`/status/1`).reply(200, statusResponse)
    var putAPI = nock('https://multiposting.stepstone.com/').filteringRequestBody(function (body) {
        console.log(body);
        return 'ABC';
    }).put('/listings/1', 'ABC').reply(201)
    console.log(nock.pendingMocks())
    sut.checkAndUpload(ycgResponse, targetJob, integrationId, jobBoard, function (err, response) {
        console.log(err)
        console.log('----')
        console.log(response)
        console.log(nock.activeMocks())
        console.log(putAPI.isDone())
        console.log(statusAPI.isDone())
        should.equal(200, response.statusCode)
    });
});

这里的问题是callback没有回叫。始终第一个GET请求通过,但PUT请求既不通过也不通过。我也没有看到任何错误。

这是我的console.logs

uploader
    #check and upload
[ 'GET https://multiposting.stepstone.com:443/status/1',
  'PUT https://multiposting.stepstone.com:443/listings/1' ]
      ✓ Should succeed in check, update and upload job status functions
Status call succeeded
{"message":"success"}
  1 passing (32ms)

0 个答案:

没有答案