Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在scheduleJob中使用Nock进行测试_Node.js_Unit Testing_Mocha.js_Nock_Node Schedule - Fatal编程技术网

Node.js 在scheduleJob中使用Nock进行测试

Node.js 在scheduleJob中使用Nock进行测试,node.js,unit-testing,mocha.js,nock,node-schedule,Node.js,Unit Testing,Mocha.js,Nock,Node Schedule,我在scheduleJob节点中测试http请求时遇到问题。好吧,我已经改变了crontime,按照建议每秒钟运行一次,以便于测试。问题是,我认为我为测试http请求而创建的Nock无法正常工作。因为当我执行console.log('res:',res);它返回未定义或不打印任何内容,如下所示:。 有人知道如何解决这个问题吗 Index.js schedule = require('node-schedule'), var dateObj = Date.now(); dateObj += 100

我在scheduleJob节点中测试http请求时遇到问题。好吧,我已经改变了crontime,按照建议每秒钟运行一次,以便于测试。问题是,我认为我为测试http请求而创建的Nock无法正常工作。因为当我执行console.log('res:',res);它返回未定义或不打印任何内容,如下所示:。 有人知道如何解决这个问题吗

Index.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
                }
            })
        })
    }
})
})


编辑 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
                }
            })
        })
    }
  })
}
*使用仍然为
res
返回未定义的
nock
测试http请求

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()
  })
})

*以下是

你能澄清什么是index.js中的
请求
实例吗?是否只是导入
请求
库?而且,您的测试从未真正调用该服务。啊,我明白为什么没有调用该服务了。我反对这种做法。拉出传递给scheduleJob的函数,并单独测试它。我已经按照您的建议编辑了问题中的代码。。但我不确定。请看一看。它仍然存在问题。您是否查看了第一次请求回调中的
err
值?如果连接超时,则会发生错误
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);
})})
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()
  })
})