相关疑难解决方法(0)

Mocha/Chai期待.没有抓住抛出的错误

我有问题让Chai expect.to.throw在我的node.js应用程序的测试中工作.测试在抛出错误时保持失败,但是如果我在try中包装测试用例并捕获并断言捕获的错误,则它可以工作.

难道expect.to.throw不喜欢的工作,我认为它应该还是什么?

it('should throw an error if you try to get an undefined property', function (done) {
  var params = { a: 'test', b: 'test', c: 'test' };
  var model = new TestModel(MOCK_REQUEST, params);

  // neither of these work
  expect(model.get('z')).to.throw('Property does not exist in model schema.');
  expect(model.get('z')).to.throw(new Error('Property does not exist in model schema.'));

  // this works
  try { 
    model.get('z'); 
  }
  catch(err) {
    expect(err).to.eql(new Error('Property does not exist in model schema.'));
  }

  done();
});
Run Code Online (Sandbox Code Playgroud)

失败:

19 …
Run Code Online (Sandbox Code Playgroud)

javascript mocha.js node.js chai

238
推荐指数
5
解决办法
13万
查看次数

标签 统计

chai ×1

javascript ×1

mocha.js ×1

node.js ×1