程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Mocha/Chai expect.to.throw不捕捉投掷错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Mocha/Chai expect.to.throw不捕捉投掷错误?

开发过程中遇到Mocha/Chai expect.to.throw不捕捉投掷错误的问题如何解决?下面主要结合日常开发的经验,给出你关于Mocha/Chai expect.to.throw不捕捉投掷错误的解决方法建议,希望对你解决Mocha/Chai expect.to.throw不捕捉投掷错误有所启发或帮助;

您必须将一个函数传递给expect。像这样:

expect(model.get.bind(model, 'z')).to.throw('Property does not exist in model schema.');
expect(model.get.bind(model, 'z')).to.throw(new Error('Property does not exist in model schema.'));

执行此操作的方式将传递给callexpect的结果model.get('z')。但是要测试是否抛出了某些东西,您必须将一个函数传递给expect,该函数expect会自行调用。bind上面使用的方法创建了一个新函数,当调用该函数时,将model.get使用this设置为的值model和设置为的第一个参数进行调用'z'

解决方法

我在让Chai的expect.to.thrownode.js应用程序进行测试时遇到问题。测试会不断导致抛出的错误,但是如果我将测试用例包装在try和catch中并断言所捕获的错误,它将起作用。

难道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();
});

失败:

19 passing (25ms)
  1 failing

  1) Model Base should throw an error if you try to get an undefined property:
     Error: Property does not exist in model schema.

大佬总结

以上是大佬教程为你收集整理的Mocha/Chai expect.to.throw不捕捉投掷错误全部内容,希望文章能够帮你解决Mocha/Chai expect.to.throw不捕捉投掷错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: