简体   繁体   English

使用MochaJS在setInterval()中测试对外部函数的回调

[英]test a callback to an outside function with in setInterval() using MochaJS

I need to test the call to the destroyTask() with in the setInterval() The setInterval() compares two time stamps and them calls the destroyTask() when they current time is equal to the finishTime 我需要在setInterval()测试对destroyTask()的调用setInterval()比较两个时间戳,当它们的当前时间等于finishTime时,它们将调用destroyTask()

 async jobTimeout() {
    if (!this.timeout || !this.timeunits) return;
    const startTime = moment();
    const finishTime = moment().add(this.timeout, this.timeunits);
    // console.log('Duration - ', this.timeout, ' ' ,this.timeunits);

    console.log(`Start Time: ${startTime}`);
    console.log(`Finish Time: ${finishTime}`);

    this._timer = setInterval(() => {
      console.log(moment());
      if (moment().isAfter(finishTime)) {
        console.log("The task didn't finish in time. ", this.destroyTask());
        clearInterval(this._timer); // the clearInterval() method clears a timer set with the setInterval() method
      }
    }, 1000);
  }

I'm using sinon to spy on two function and fake timers to overwrite setInterval . 我正在使用sinon监视两个函数,使用伪造的计时器覆盖setInterval My test case is bellow: 我的测试用例如下:

 describe('Generic Setup', () => {
    beforeEach(() => {
      // Overwrite the global timer functions (setTimeout, setInterval) with Sinon fakes
      this.clock = sinon.useFakeTimers();
    });

    afterEach(() => {
      // Restore the global timer functions to their native implementations
      this.clock.restore();
    });

    it('Job Should Timeout After Specified Duration', async () => {
      const task = new TASK(taskData, done);
      const result = sinon.spy(await task.jobTimeout());
      const destroyTask = sinon.spy(await task.destroyTask());

      setInterval(result, 1000);

      this.clock.tick(4000);
      expect(destroyTask).to.not.have.been.called;

      this.clock.tick(1000);
      expect(destroyTask).to.have.been.called;
    });
  });

I receive the following error TypeError: this.done is not a function . 我收到以下错误TypeError: this.done is not a function I think it's caused by destroyTask , but it is a function. 我认为这是由destroyTask引起的,但这是一个函数。

async destroyTask() {
    // TODO cleanup tmp directory - might want to collect stat on error
    clearInterval(this._timer);
    this.done();
  }

What can be the cause? 可能是什么原因? Is it possible to test callback to outside function with in another function? 是否可以在另一个函数中测试对外部函数的回调?

Had to modify a few things. 不得不修改一些东西。 I've changed the spy call, increased the second this.clock.tick() to 2000 and had to give a custom done function in class instantiation. 我更改了间谍调用,将第二个this.clock.tick()增加到2000并且必须在类实例化中提供自定义的done函数。

  describe('Generic Setup', () => {
    beforeEach(() => {
      // Overwrite the global timer functions (setTimeout, setInterval) with Sinon fakes
      this.clock = sinon.useFakeTimers();
    });

    afterEach(() => {
      // Restore the global timer functions to their native implementations
      this.clock.restore();
    });

    it('Job Should Timeout After Specified Duration', async () => {
      const task = new TASK(taskData, () => {});

      task.jobTimeout();

      const destroyTaskSpy = sinon.spy(task, 'destroyTask');

      this.clock.tick(4000);
      expect(destroyTaskSpy).to.not.have.been.called;

      this.clock.tick(2000);
      expect(destroyTaskSpy).to.have.been.called;

      // Remove the spy to prevent future errors
      destroyTaskSpy.restore();
    });
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM