Rad*_*ski 3 javascript unit-testing assert qunit throws
我用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'");
});
Run Code Online (Sandbox Code Playgroud)
第一次和第二次测试通过,因为通过函数(,, slide -...")解析的动画类是可以的.但第三次测试死了:
Died on test #3 at file...
Run Code Online (Sandbox Code Playgroud)
因为它抛出RangeError.它可以抛出RangeError,但我不明白如何在单元测试中捕获它,获取,确定"信息.如果我理解QUnit文档:QUnit抛出文档
我做的一切都好:我传递抛出错误的函数,预期错误的实例和预期的消息.我有人会决定帮助我,我会很高兴 - 提前谢谢你.
我自己找到了答案.而是调用函数:
assert.throws(myOwnCreator.resolveAnimationType("slideSide", "hide"),
new RangeError()....
Run Code Online (Sandbox Code Playgroud)
我应该传递给assert.throws()函数调用我的函数,如下所示:
assert.throws(function(){
myOwnCreator.resolveAnimationType("slideSide", "hide");
},
new RangeError(--message which function should throw in error--)...
Run Code Online (Sandbox Code Playgroud)