0

我在 scheduleJob 节点中测试 http 请求时遇到问题。好吧,我已经按照这里的建议将 crontime 更改为每秒运行一次,以便于测试。问题是,我不认为我为测试 http 请求而创建的 Nock 工作正常。因为当我做 console.log('res : ', res); 它要么返回未定义,要么不打印任何内容,如下所示:在此处输入图像描述。有谁知道如何解决这个问题?

索引.js

schedule = require('node-schedule'),
var dateObj = Date.now();
dateObj += 1000;
schedule.scheduleJob(new Date(dateObj), function(){
console.log("get's here..");
const url_req = "https://git.ecommchannels.com/api/v4/users";
request.get(
    {
     url_req,
     qs : {private_token: token, per_page: 10}
}, function(err, res, body){
    console.log('res : ', res);
    var total = res.headers['x-total-pages']
    console.log('res.headers', res.headers);
    for( i=1; i<=total; i++ ){
        url_req2 = "https://git.ecommchannels.com/api/v4/users";
        request.get({
            url_req2,
            qs: {private_token : token, per_page : 10, page : i},
            json : true
        },
            (err2, res2, body2) => {
            body2.forEach(element => {
                if(condition){
                    //do something
                }
            })
        })
    }
})

});

单元测试:

describe('test the http request', function(){
var clock;
const nockingGit = () =>{
    nock('https://git.ecommchannels.com')
    .defaultReplyHeaders({
        'x-total-pages': 10
    })
    .get('/api/v4/users')
    .query({private_token: 'blabla', per_page: 10})
    .reply(201, {});
}
beforeEach(function () {
  clock = sinon.useFakeTimers();
});

afterEach(function () {
  clock.restore();
});
it('test the http request', (done)=>{
    nockingGit();
    setTimeout(function(){
        done();
    },3000)
    clock.tick(3000);
})

})


已编辑

index.js

schedule.scheduleJob(new Date(dateObj), function(){
  var now = new Date();
  flag = true;
  console.log(now.toLocaleString('en-US', { timeZone: 'Asia/Jakarta'}));
  console.log("get's here..");
  gitReq();
});

function gitReq(){
const url_req = "https://git.ecommchannels.com/api/v4/users";
request.get(
  {
     url_req,
     qs : {private_token: token, per_page: 10}
  }, function(err, res, body){
    console.log('res : ', res);
    var total = res.headers['x-total-pages']
    // console.log('res.headers', res.headers);
    for( i=1; i<=total; i++ ){
        url_req2 = "https://git.ecommchannels.com/api/v4/users";
        request.get({
            url_req2,
            qs: {private_token : token, per_page : 10, page : i},
            json : true
        },
            // "https://git.ecommchannels.com/api/v4/users?private_token=" + token + "&per_page=10&page=" + i, { json: true }, 
            (err2, res2, body2) => {
            body2.forEach(element => {
                if(condition){
                    //do something
                }
            })
        })
    }
  })
}

单元测试

*检查是否gitReq()调用-使用sinon.spy(但我不确定实现是否正确)

const sendReq = {
gitReq: ()=>{}}

describe('test the schedule job', function(){
var clock;
beforeEach(function () {
  clock = sinon.useFakeTimers();
});

afterEach(function () {
  clock.restore();
});
it('should call the gitReq once', (done)=>{
    let gitReq = sinon.spy(sendReq,"gitReq");
    sendReq.gitReq();
    console.log("callcount : ", gitReq.callCount);
    gitReq.restore();
    setTimeout(function(){
        expect(gitReq.calledOnce).to.be.true;
        done();
    },3000)
    clock.tick(3000);
})})

*使用它测试 http 请求nock仍然返回未定义的res.

describe('test the http request', function(){
  const nockingGit = () =>{
    nock('https://git.ecommchannels.com')
      .defaultReplyHeaders({
        'x-total-pages': 10
      })
      .get('/api/v4/users')
      .query({private_token: 'UKz2cZP9CHz2DT_o2-s9', per_page: 10})
      .reply(304, {});
  }
  it('test the http request', function(){
    nockingGit();
    _import.gitReq()
  })
})

*这是测试结果

4

0 回答 0