摩卡:测试某事物是否比一段时间更多*

时间:2014-11-26 11:31:18

标签: javascript testing mocha

如果您想在一定时间内测试某些内容,可以使用.timeout()功能:

it('should have preloaded the first image', function () {
  this.timeout(10);

  return promise.then(function (data) {
    return Monkey.helpers.preload(data.urls[0]);
  });
});

但是,反过来测试并不是那么容易:如果我想测试任务是否需要更多超过30ms,我必须使用超时:

it('should not have preloaded the tenth image', function (cb) {
  return promise.then(function (data) {
    var finished = false;
    Monkey.helpers.preload(data.urls[10])
      .then(function () {
        finished = true;
      })

    setTimeout(function () {
      finished.should.be.False;
      cb();
    }, 30);
  });
});

这不是很简单!我更喜欢这样的事情:

it('should not have preloaded the tenth image', function (cb) {
  this.shouldTake(30);

  return promise.then(function (data) {
    return Monkey.helpers.preload(data.urls[10]);
  });
});

有人知道更简单的方法吗?

0 个答案:

没有答案
相关问题