简体   繁体   English

Chai期望抛出错误信息

[英]Chai expect to throw with error message

I'm trying to test the message of an Error using expect().to.throw . 我正在尝试使用expect().to.throw来测试Error的消息。 I read that expect needs to be passed a function to execute, as oppose to the function that returns. 我读到期望需要传递一个函数来执行,而不是返回的函数。 Anyways, I still can't seem to get this to work, I've tried a number of variations, but this is the basic setup in jsfiddle . 无论如何,我似乎仍然无法让这个工作,我尝试了很多变化,但这是jsfiddle的基本设置。 Any ideas why this won't work or how can I fix it? 任何想法为什么这不起作用或如何解决它?

Thank you 谢谢

The issue was that I was using throw to check, but I wasn't actually throwing an error, but returning an Error . 问题是我使用throw来检查,但我实际上并没有抛出错误,而是返回Error The following evaluates correctly: 以下评估正确:

expect(test.throwFunction()).to.be.an.instanceOf(Error).with.property('message', 'This is an error message')

However, I'm not sure this is the cleanest way to check - seems like there could be a shorthand version. 但是,我不确定这是最干净的检查方法 - 似乎可能有速记版本。

Update: Looks like there's a work in progress for adding to.be.an.error 更新:看起来正在进行添加to.be.an.error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM