0
我需要调用expect().to.throw()
上的一个带参数的函数。例如,假设我有以下几点:node:使用chai的expect.to.throw在一个带参数的函数上
var func = function(x) {
if (x > 1) {
throw new Error()
} else {
console.log('hello')
}
}
expect(func).to.throw()
这失败,因为它要求func
不带参数,这意味着它不会抛出一个错误。但如果我拨打expect(func(2)).to.throw()
,我会得到AssertionError: expected undefined to be a function
。该函数被调用并且返回值被传递给expect
。
如何使用expect().to.throw()
并给出包含的函数参数。是唯一的方法来做到这一点与匿名功能,如expect(() => {func(2)}).to.throw()
?
'bind()的'也是一个不错的选择:'希望(fn.bind(NULL,42))to.throw()' –