如何使用mocha,promises和catch块来防止误报

时间:2015-06-06 18:43:54

标签: javascript promise mocha

我想利用Mocha的内置承诺支持,但是当我想在我的承诺链中测试catch分支时,我很难处理误报。

This问题与我想要的最接近,但解决方案要求我的项目中的每个开发人员都添加一个会引发错误的then,并确保该错误不会意​​外地通过测试

因此,我不得不恢复使用done样式的测试,它依赖于内置的超时来捕获错误而不是断言。这不太理想,但消除了误报的可能性。

var RSVP = require('RSVP');

function request (shouldSucceed) {
  if (shouldSucceed) {
    return RSVP.Promise.resolve('success');
  } else {
    return RSVP.Promise.reject(new Error('failure'));
  }
}

describe('request', function () {
  it('throws an error if shouldSucceed is not provided', function () {
    // this test is incorrectly calling `request`
    // which leads to it passing without actually testing
    // the behaviour it wants to
    return request(true)
      .catch(function (err) {
        expect(err).to.be.an.instanceof(Error)
      });
  });

  it('throws an error if shouldSucced is not provided (manual then block)', function () {
    // this test tacks a `then` onto the chain, to ensure `request`
    // actually throws an error as expected
    // unfortunately, it still passes since the assertion needs to do
    // a little extra work to ensure the type of error thrown is the
    // expected error
    return request(true)
      .then(function () {
        throw new Error('Not expected');
      })
      .catch(function (err) {
        expect(err).to.be.an.instanceof(Error)
      });
  });

  it('throws an error if shouldSucceed is not provided (done syntax)', function (done) {
    // this assertion fails (as it should)
    // due to a timeout
    return request(true)
      .catch(function () {
         expect(err).to.be.an.instanceof(Error);
         done()
      });
  });
});

输出:

  request
    ✓ throws an error if shouldSucceed is not provided
    ✓ throws an error if shouldSucced is not provided (manual then block)
    1) throws an error if shouldSucceed is not provided (done syntax)


  2 passing (2s)
  1 failing

  1) request throws an error if shouldSucceed is not provided (done syntax):
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

是否有一种更清晰的方式告诉摩卡我希望在catch块中发生某些事情,并且成功解决承诺应该是测试失败?

1 个答案:

答案 0 :(得分:1)

您正在寻找

it('request(false) should throw an error', function () {
    return request(false).then(function() {
        throw new Error('unexpected success');
    }, function(err) {
        expect(err).to.be.an.instanceof(Error)
    });
});

请参阅When is .then(success, fail) considered an antipattern for promises?,了解您目前所拥有代码的差异。