bra*_*ipt 2 javascript mocha.js node.js should.js
我有一个错误抛出:
if (somethingBadHappened) {
throw new Error('what were you thinking, batman?')
}
Run Code Online (Sandbox Code Playgroud)
现在我想编写一个测试来检查这是否会在预期时抛出:
should(MyClass.methodThatShouldThrow()).throws('what were you thinking, batman?')
Run Code Online (Sandbox Code Playgroud)
但这实际上抛出了错误.我究竟做错了什么?Should.js文档非常简单,因为它继承自assert.throws,但即使是文档也不能以'should.js'的方式工作?
正如您所发现的那样,您的代码执行了抛出错误should并且没有机会捕获它的函数.
要允许正确的断言,您需要将函数调用包装在匿名函数中.
should(function () {MyClass.methodThatShouldThrow();} ).throw('what were you thinking, batman?')
Run Code Online (Sandbox Code Playgroud)