1

我已经定义了以下简单的函数,我想用 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 });
    });
}

下面是我的诺克设置。

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)
4

0 回答 0