我的Mocha测试第一次传递,但在使用手表

时间:2017-02-17 16:34:41

标签: mocha integration-testing chai

我正在使用mocha和chai编写集成测试。我想使用mocha -w但是每当代码由于更改而重新运行时,测试都会失败并显示未调用done的消息。但是,每次我停止摩卡并再次运行它运行正常。似乎存在某种类型的范围问题,当摩卡重新运行时,某些事情超出了范围。任何帮助将不胜感激!

使用Mocha“mocha@3.2.0”

function sendGoodMsg(){
  return sendMsg(payloads.goodPayload, "good_msg")
}

function tooManyMsg(){
  return sendMsg(payloads.tooManyPayload, "too_many_msg")
}
function tooFewMsg(){
  return sendMsg(payloads.tooFewPayload, "too_few_msg")
}
function sendMsg(payload, type){
  //get process started
  let sentMsg = new queue.CCMessage( 'scheduler_service', queue.WORK, 'setSchedule' );
  sentMsg.context.origContext = uuid.v4();
  sentMsg.context.testType = type;
  sentMsg.payload = payload;
  return sentMsg.Send();
}

function receiveMsg(payloadType){
  return new Promise(resolve =>{
    const messageReceiver = new queue.MessageReceiver(queue.THIS_SERVICE, queue.WORK);
    messageReceiver.on('setSchedule', (msg)=>{
      if(msg.context.testType === payloadType){
        resolve(msg.payload);
        msg.ACK();
      } else {
        msg.NAK();
      }
    });
    messageReceiver.Listen();
  })
}

describe("should receive sent message", function(){
  it('should have the following fields', function () {
    return sendGoodMsg().then(()=>{
      return receiveMsg("good_msg").then(payload =>{
        return assert.isTrue(processMessage.messageIsValid(payload));
      })
    })
  })
  it('should not have extra fields', function () {
    return tooManyMsg().then(()=> {
      return receiveMsg("too_many_msg").then(payload => {
        return assert.isFalse(processMessage.messageIsValid(payload));
      })
    })
  })
  it('should not be missing fields', function () {
    return tooFewMsg().then(()=> {
      return receiveMsg("too_few_msg").then(payload => {
        return assert.isFalse(processMessage.messageIsValid(payload));
      })
    })
  })
});

0 个答案:

没有答案