首页 文章

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

提问于
浏览
1

当我想在我的诺言链中测试 catch 分支时,我想利用Mocha 's built in promise support, but i' m难以处理误报 .

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 回答

  • 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? .

相关问题