2014-10-18 26 views
2

我做了一些Javascript单元测试与QUnit库,然后我想测试如果方法抛出RangeError。它以这种方式来完成:测试如果JS方法抛出RangeError与QUnit

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'"); 
}); 

第一和第二测试通过,因为动画类的函数解析(,,幻灯片-...“)被确定,但第三次测试而死。

Died on test #3  at file... 

因为它会引发RangeError,可以引发RangeError,但我不明白我怎么能在单元测试中捕获它,以获得“ok”信息。如果我了解QUnit文档:QUnit throws documentation

我做的一切正常:我传递函数抛出错误,预期错误的实例和预期的消息。我有人会决定帮助我,我会很高兴 - 提前谢谢你。

回答

4

我自己找到了答案。作为调用函数:

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--)... 
+1

这就是它!很高兴你找到了它。 – jakerella 2014-10-18 23:12:01