我使用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,但我不明白如何在单元测试中捕获它,以获得,确定"信息。如果我理解QUnit文档:QUnit throws documentation
我做的一切都好:我传递抛出错误的函数,预期错误的实例和预期的消息。我有人会决定帮助我,我会很高兴 - 谢谢你。
答案 0 :(得分:5)
我自己找到了答案。而是调用函数:
assert.throws(myOwnCreator.resolveAnimationType("slideSide", "hide"),
new RangeError()....
我应该传递给assert.throws()函数,它调用我的函数,如下所示:
assert.throws(function(){
myOwnCreator.resolveAnimationType("slideSide", "hide");
},
new RangeError(--message which function should throw in error--)...