I am new to Qunit and unit testing.
I am trying to figure out what and how to test the following function. It does not do much at the moment but I wanted to assert that if I pass it incorrect values that errors are being thrown:
function attrToggle (panel, attr) {
'use strict';
if (!panel) { throw new Error('Panel is not defined'); }
if (!attr) { throw new Error('Attr is not defined'); }
if (typeof panel !== 'string') { throw new Error('Panel is not a string'); }
if (typeof attr !== 'string') { throw new Error('Attr is not a string'); }
if (arguments.length !== 2) { throw new Error('There should be only two arguments passed to this function')}
};
How do I go about asserting that an error will be thrown if any of these conditions are not met?
I tried to look at Qunit's 'raises' assertion but think I am misunderstanding it. My interpretation would be that the test passes if an error is thrown.
So if I tested something like this:
test("a test", function () {
raises(function () {
throw attrToggle([], []);
}, attrToggle, "must throw error to pass");
});
The test should pass because errors are thrown.