Javascript 测试 Mocha 中抛出的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14966821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:29:36  来源:igfitidea点击:

Testing for errors thrown in Mocha

javascripttestingmochachai

提问by Chris Neitzer

I'm hoping to find some help with this problem. I'm trying to write tests for an application I am writing. I have distilled the problem in to the following sample code. I want to test that an error was thrown. I'm using Testacular as a test runner with mocha as the framework and chai as the assertion library. The tests run, but the test fails because an error was thrown! Any help is greatly appreciated!

我希望能在这个问题上找到一些帮助。我正在尝试为我正在编写的应用程序编写测试。我已将问题提炼为以下示例代码。我想测试是否抛出了错误。我使用 Testacular 作为测试运行器,mocha 作为框架,chai 作为断言库。测试运行,但测试失败,因为抛出了错误!任何帮助是极大的赞赏!

function iThrowError() {
    throw new Error("Error thrown");
}

var assert = chai.assert,
    expect = chai.expect;
describe('The app', function() {
    describe('this feature', function() {
        it("is a function", function(){
            assert.throw(iThrowError(), Error, "Error thrown");
        });
    });
});

回答by red

You're not passing your function to assert.throws()the right way.

你没有以assert.throws()正确的方式传递你的函数。

The assert.throws()function expects a function as its first parameter. In your code, you are invoking iThrowError and passing its return value when calling assert.throws().

assert.throws()函数需要一个函数作为它的第一个参数。在您的代码中,您正在调用 iThrowError 并在调用assert.throws().

Basically, changing this:

基本上,改变这个:

assert.throws(iThrowError(), Error, "Error thrown");

to this:

对此:

assert.throws(iThrowError, Error, "Error thrown");

should solve your problem.

应该可以解决您的问题。

With args:

使用参数:

assert.throw(() => { iThrowError(args) }, Error);

or

或者

assert.throw(function() { iThrowError(args) }, Error, /Error thrown/);

回答by Jake

Adding to the top answer, if you need to invoke your function as part of the test (i.e. your function should only throw an error if certain parameters are passed), you can wrap your function call in an anonymous function, or, in ES6+, you can pass your function in an arrow function expression.

添加到顶部答案,如果您需要调用您的函数作为测试的一部分(即您的函数应该只在传递某些参数时抛出错误),您可以将您的函数调用包装在匿名函数中,或者,在 ES6+ 中,您可以在箭头函数表达式中传递您的函数。

// Function invoked with parameter.
// TEST FAILS. DO NOT USE.
assert.throws(iThrowError(badParam), Error, "Error thrown"); // WRONG!

// Function invoked with parameter; wrapped in anonymous function for test.
// TEST PASSES.
assert.throws(function () { iThrowError(badParam) }, Error, "Error thrown");

// Function invoked with parameter, passed as predicate of ES6 arrow function.
// TEST PASSES.
assert.throws(() => iThrowError(badParam), Error, "Error thrown");

And, just for the sake of thoroughness, here's a more literal version:

而且,为了彻底起见,这里有一个更字面的版本:

// Explicit throw statement as parameter. (This isn't even valid JavaScript.)
// TEST SUITE WILL FAIL TO LOAD. DO NOT USE, EVER.
assert.throws(throw new Error("Error thrown"), Error, "Error thrown"); // VERY WRONG!

// Explicit throw statement wrapped in anonymous function.
// TEST PASSES.
assert.throws(function () { throw new Error("Error thrown") }, Error, "Error thrown");

// ES6 function. (You still need the brackets around the throw statement.)
// TEST PASSES.
assert.throws(() => { throw new Error("Error thrown") }, Error, "Error thrown");

回答by Vincent Simard

I saw you were able to resolve your problem but were not able to check for a specific error. To do so using Chai's expect/should syntax, you can use the parameters from the different signatures of throw():

我看到您能够解决您的问题,但无法检查特定错误。为此,使用 Chai 的 expect/should 语法,您可以使用来自throw()不同签名的参数:

@param{ ErrorConstructor } constructor
@param{ String | RegExp } expectederror message
@param{ String } message _optional_

In your example, you should be able to use either of the following:

在您的示例中,您应该能够使用以下任一项:

expect(iThrowError).to.throw(/Error thrown/);
expect(iThrowError).to.throw(Error, /Error thrown/);
expect(iThrowError).to.throw(new Error('Error thrown'));

And (again, from chai's documentation), you could filter other error messages using:

并且(同样,来自 chai 的文档),您可以使用以下方法过滤其他错误消息:

expect(iThrowError).to.throw(Error).and.not.throw(/Another Error thrown/);

Hope this helps!

希望这可以帮助!