Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用QUnit测试JS方法是否抛出RangeError_Javascript_Unit Testing_Assert_Qunit_Throws - Fatal编程技术网

Javascript 使用QUnit测试JS方法是否抛出RangeError

Javascript 使用QUnit测试JS方法是否抛出RangeError,javascript,unit-testing,assert,qunit,throws,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"),

我用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文档:


我一切都做得很好:我传递了抛出错误的函数、预期错误的实例和预期消息。如果有人愿意帮助我,我会非常高兴的——提前谢谢你。

我自己找到了答案。而不是调用函数:

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

就这样!很高兴你找到了。