我如何才能测试以下功能是否被开玩笑?

时间:2018-07-06 11:02:07

标签: reactjs jestjs

在下面的函数中,我在用spyon模拟它并希望已经调用之后用笑话编写了一个测试用例,但是覆盖范围并未消除。

startTimerAndPassIntent(){
        let time;
        let timeMsg = '';
        if(this.counter < 3) {
            if(this.counter == 1){
                time = this.timer1;
                timeMsg = 'timer1';
            } else if (this.counter == 2){
                time = this.timer2;
                timeMsg = 'timer2';
            } else {
                console.log(this.counter);
            }
            this.clearTimeOutObj = setTimeout(() => {
                this.postMessageData(timeMsg);
                console.log(timeMsg);
                this.counter ++ ;
            },time);
        }
        return timeMsg;
    }

我的测试用例:

it('there is a new timemsg',() => {
  const wrapper = mount(
    <Chatroom />
  );
  const spy = sinon.spy(wrapper.instance(), 'startTimerAndPassIntent');
  // call the function being tested
  wrapper.instance().postMessageData('Hi');

  setTimeout(() => {
      // check if spy was called
      sinon.assert.called(spy);
      done();
  }, 10);


});

0 个答案:

没有答案