测试 Mocha 中抛出的错误

我希望能在这个问题上找到一些帮助。我正在尝试为我正在编写的应用程序编写测试。我已经将这个问题浓缩到下面的示例代码中。我想测试是否抛出了错误。我使用 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");
});
});
});
122656 次浏览

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():

@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:

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

Hope this helps!

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

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().

Basically, changing this:

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

to this:

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

should solve your problem.

With args:

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

or

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

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.

// 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");