测试JS方法是否使用QUnit引发RangeError [英] Testing if JS method throws RangeError with QUnit

查看:98
本文介绍了测试JS方法是否使用QUnit引发RangeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用QUnit库进行了一些Javascript单元测试,然后想测试方法是否抛出RangeError.可以通过以下方式完成:

I made some Javascript unit tests with QUnit library, then I want to test if method throws RangeError. It has be done in this way:

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 -...)解析的动画类还可以.但是第三次​​测试失败了:

First and second test passed, because animation classes resolved by function (,,slide-...") were ok. But third test died:

Died on test #3     at file...

因为它引发RangeError.可以抛出RangeError是可以的,但是我不明白如何在单元测试中捕获它,以获得确定"的信息.如果我了解QUnit文档:

Because it throws RangeError. It's ok that it throws RangeError, but I don't understand how I can catch it in unit test, to get ,,ok" information. If I understand QUnit documentation: QUnit throws documentation

我一切正常:我传递了引发错误,预期错误实例和预期消息的函数.我会决定帮助我,我会很高兴-谢谢您.

I do everything ok: I pass function which throws Error, instance of expected Error and expected message. I anybody would decide to help me, I will be very happy - thank you in advance.

推荐答案

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

I found an answer on my own. Instead calling function:

assert.throws(myOwnCreator.resolveAnimationType("slideSide", "hide"), 
  new RangeError()....

我应该传递给assert.throws()函数,该函数将调用我的函数,如下所示:

I should pass to assert.throws() function which call my function, like this:

assert.throws(function(){
    myOwnCreator.resolveAnimationType("slideSide", "hide");
  }, 
  new RangeError(--message which function should throw in error--)...

这篇关于测试JS方法是否使用QUnit引发RangeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆