小编Apz*_*pzx的帖子

柴:根据参数预期错误与否

我一直在尝试做一个函数的文本来处理错误,如果它是一个有效的错误,它会被抛出,但如果不是,那么什么都不会抛出.问题是我似乎无法在使用时设置参数:

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)

javascript mocha.js chai

21
推荐指数
2
解决办法
8017
查看次数

标签 统计

chai ×1

javascript ×1

mocha.js ×1