How to assert that a function does not raise an exception
Asked Answered
U

2

19

QUnit has an assertion for testing that a function raises an exception (QUnit/raises). Is it possible -- using QUnit -- to assert that a function does not raise an exception.

I realize that it is possible to test it like in the following code:

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

But I think it ought to be possible using QUnit. Any clues?

Unidirectional answered 22/3, 2012 at 12:34 Comment(1)
check this #10190892 it can helpViscosity
L
18

There is no such method in qunit

However, if you just write the following code which is much shorter, you will obtain the same result with additionnal benefits

theTest();
ok(true, "My function does not crash");

1/ If the code of a test raises an exception, qunit will mark the test as failed.

2/ If you check the "no try/catch" checkbox, you will be able to debug where the exception was thrown, which is not the case with your try/catch

Leapt answered 22/3, 2012 at 12:42 Comment(4)
Of course! That's pretty simple. And apparently a bit too simple for me to figure out. Thanks for answering :)Unidirectional
As it turns out, this was a bit too simple. If the function raises an exception the test fails without a failure message. If the function do not raise an exception the test succeeds as expected. The message in the ok()-assertion is never used.Unidirectional
Note that you should probably also add a call to expect() inside your test() function so that the number of assertions can also be verified that way.Tempura
the ok() assertion is required, as a test without assertions will fail in qunitObscurant
B
2

I had the same issue as you mentioned in the comment whereby my test which tests no Error is thrown would stop "badly" showing a badly formatted Died on test #1 message without any useful information.

I ended up using a mix of both; raises() for one test and try/catch for the other.

I used raises() for the test which tests that an Error is thrown, similar to this:

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

If the above throws an Error all is fine and if not a nice formatted message is displayed, similar to this:

myFunction() should throw Error
Result: No exception was thrown.

I then used try/catch for the tests which have to ensure no Error is thrown, similar to this:

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

If the above throws no Error all is fine and if an Error is thrown a nice formatted message is displayed, similar to this:

myFunction() should not throw an Error
Source: ...
Bizerte answered 5/12, 2013 at 12:15 Comment(1)
The above is based on the assumption that you are testing managed custom thrown errors and your myFunction() does do an intentional throw new Error(...) given a certain invalid condition. You should not test for unexpected errors off course.Bizerte

© 2022 - 2024 — McMap. All rights reserved.