Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 如何断言函数不会引发异常_Javascript_Unit Testing_Testing_Qunit - Fatal编程技术网

Javascript 如何断言函数不会引发异常

Javascript 如何断言函数不会引发异常,javascript,unit-testing,testing,qunit,Javascript,Unit Testing,Testing,Qunit,QUnit有一个断言,用于测试函数是否引发异常()。是否有可能——使用QUnit——断言函数不会引发异常 我意识到可以像在以下代码中那样对其进行测试: try { theTest(); ok(true); } catch (e) { ok(false, "Expected to succeed"); } 但我认为使用QUnit应该是可能的。有什么线索吗?昆特没有这种方法 但是,如果您只编写以下短得多的代码,您将获得同样的结果,并带来额外的好处 theTest(); ok

QUnit有一个断言,用于测试函数是否引发异常()。是否有可能——使用QUnit——断言函数不会引发异常

我意识到可以像在以下代码中那样对其进行测试:

try {
    theTest();
    ok(true);
} catch (e) {
    ok(false, "Expected to succeed");
}

但我认为使用QUnit应该是可能的。有什么线索吗?

昆特没有这种方法

但是,如果您只编写以下短得多的代码,您将获得同样的结果,并带来额外的好处

theTest();
ok(true, "My function does not crash");
1/如果测试代码引发异常,qunit会将测试标记为失败


2/如果您选中“no try/catch”复选框,您将能够调试引发异常的位置,而您的try/catch情况并非如此。我遇到了与您在评论中提到的问题相同的问题,即我的测试不会引发
错误
将“严重”停止在没有任何有用信息的情况下,显示格式错误的
在测试#1
消息

我最终使用了两者的混合
raises()
用于一个测试,而
try/catch
用于另一个测试

我在测试中使用了raises(),测试是否抛出了
错误
,如下所示:

test("When myFunction() is called with a invalid instance Then Error is thrown", function () {
    // Arrange
    var testInstance = {};

    // Act
    raises(function() {
        myFunction(testInstance);
    }, Error, "myFunction() should throw an Error");

    // Assert
    // raises() does assertion
});
myFunction() should throw Error
Result: No exception was thrown.
test("When myFunction() is called with a valid instance Then no Error is thrown", function () {
    // Arrange
    var testInstance = new myObject();
    var result;

    // Act
    try {
        myFunction(testInstance);
        result = true;
    } catch(error) {
        result = false;
    }

    // Assert
    ok(result, "myFunction() should not throw an Error"); 
});
myFunction() should not throw an Error
Source: ...
如果上面抛出一个
错误
则一切正常,如果不是,则显示一条格式良好的消息,类似于此:

test("When myFunction() is called with a invalid instance Then Error is thrown", function () {
    // Arrange
    var testInstance = {};

    // Act
    raises(function() {
        myFunction(testInstance);
    }, Error, "myFunction() should throw an Error");

    // Assert
    // raises() does assertion
});
myFunction() should throw Error
Result: No exception was thrown.
test("When myFunction() is called with a valid instance Then no Error is thrown", function () {
    // Arrange
    var testInstance = new myObject();
    var result;

    // Act
    try {
        myFunction(testInstance);
        result = true;
    } catch(error) {
        result = false;
    }

    // Assert
    ok(result, "myFunction() should not throw an Error"); 
});
myFunction() should not throw an Error
Source: ...
然后,我使用
try/catch
进行测试,这些测试必须确保不会抛出
Error
,如下所示:

test("When myFunction() is called with a invalid instance Then Error is thrown", function () {
    // Arrange
    var testInstance = {};

    // Act
    raises(function() {
        myFunction(testInstance);
    }, Error, "myFunction() should throw an Error");

    // Assert
    // raises() does assertion
});
myFunction() should throw Error
Result: No exception was thrown.
test("When myFunction() is called with a valid instance Then no Error is thrown", function () {
    // Arrange
    var testInstance = new myObject();
    var result;

    // Act
    try {
        myFunction(testInstance);
        result = true;
    } catch(error) {
        result = false;
    }

    // Assert
    ok(result, "myFunction() should not throw an Error"); 
});
myFunction() should not throw an Error
Source: ...
如果上面没有抛出
错误
则一切正常,如果抛出
错误
,则会显示一条格式良好的消息,如下所示:

test("When myFunction() is called with a invalid instance Then Error is thrown", function () {
    // Arrange
    var testInstance = {};

    // Act
    raises(function() {
        myFunction(testInstance);
    }, Error, "myFunction() should throw an Error");

    // Assert
    // raises() does assertion
});
myFunction() should throw Error
Result: No exception was thrown.
test("When myFunction() is called with a valid instance Then no Error is thrown", function () {
    // Arrange
    var testInstance = new myObject();
    var result;

    // Act
    try {
        myFunction(testInstance);
        result = true;
    } catch(error) {
        result = false;
    }

    // Assert
    ok(result, "myFunction() should not throw an Error"); 
});
myFunction() should not throw an Error
Source: ...

当然这很简单。显然有点太简单了,我想不出来。谢谢你的回答:)事实证明,这有点太简单了。如果函数引发异常,则测试将失败,且不会显示失败消息。如果函数未引发异常,测试将按预期成功。从未使用过ok()-断言中的消息。请注意,您可能还应该在test()函数中添加对expect()的调用,以便也可以通过这种方式验证断言的数量。需要ok()断言,由于没有断言的测试将失败,上述假设基于您正在测试托管的自定义抛出错误,并且您的
myFunction()
在给定某个无效条件的情况下,确实有意抛出新错误(…)。您不应在测试过程之外测试意外错误。请检查此选项,这会有所帮助