我用 QUnit 库做了一些 Javascript 单元测试,然后我想测试方法是否抛出 RangeError。它是通过这种方式完成的:
QUnit.test("Resolve slide animation from left", function( assert ) {
var myOwnCreator = new MyOwnCreator();
assert.equal(myOwnCreator.resolveAnimationType("slideSide", "show", "left"),
"slide-left-in");
assert.equal(myOwnCreator.resolveAnimationType("slideSide", "hide", "left"),
"slide-left-out");
assert.throws(myOwnCreator.resolveAnimationType("slideSide", "hide"),
new RangeError(),
" If animation type is 'slideSide', you have to provide correct direction"
+ "to identify animation properly, direction cannot be 'undefined'");
});
第一个和第二个测试通过了,因为由函数 (,,slide-...") 解析的动画类没问题。但是第三个测试死了:
Died on test #3 at file...
因为它抛出 RangeError。抛出 RangeError 没关系,但我不明白如何在单元测试中捕获它,以获取“,,ok”信息。如果我了解 QUnit 文档:QUnit throws documentation
我做的一切都很好:我传递了抛出错误的函数、预期错误的实例和预期的消息。我任何人都会决定帮助我,我会很高兴 - 提前谢谢你。