我有一个错误抛出如下:
if (somethingBadHappened) {
throw new Error('what were you thinking, batman?')
}现在,我想编写一个测试,以检查这是否在预期时抛出:
should(MyClass.methodThatShouldThrow()).throws('what were you thinking, batman?')但这实际上会造成错误。我做错了什么?Should.js文档很轻,因为它是从assert.throws继承的,但是即使是它的文档也不能以“should.js”的方式工作?
发布于 2015-11-20 20:04:21
正如您已经发现的,您的代码执行抛出错误的函数& should没有机会捕捉它。
为了允许正确的断言,您需要将函数调用包装在一个匿名函数中。
should(function () {MyClass.methodThatShouldThrow();} ).throw('what were you thinking, batman?')https://stackoverflow.com/questions/33835127
复制相似问题