我一直在尝试做一个函数的文本来处理错误,如果它是一个有效的错误,它会被抛出,但如果不是,那么什么都不会抛出.问题是我似乎无法在使用时设置参数:
expect(handleError).to.throw(Error);
Run Code Online (Sandbox Code Playgroud)
理想的是使用:
expect(handleError(validError)).to.throw(Error);
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个功能?
功能代码:
function handleError (err) {
if (err !== true) {
switch (err) {
case xxx:
...
}
throw "stop js execution";
else {}
}
Run Code Online (Sandbox Code Playgroud)
和测试代码(不按预期工作):
it("should stop Javascript execution if the parameter isnt \"true\"", function() {
expect(handleError).to.be.a("function");
expect(handleError(true)).to.not.throw(Error);
expect(handleError("anything else")).to.throw(Error);
});
Run Code Online (Sandbox Code Playgroud)