我是新手Qunit和單元測試。聲明函數拋出異常與Qunit
我想弄清楚如何測試以下功能。它並沒有做太多的時刻,但我想斷言,如果我將它傳遞不正確的值正在引發的錯誤:
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')}
};
如何去斷言的時候,如果這些條件的任何被拋出沒有滿足?
我試圖去看看Qunit的'提出'斷言,但認爲我誤解了它。我的解釋是,如果錯誤發生,測試通過。
所以如果我測試過這樣的事情:
test("a test", function() {
raises(function() {
throw attrToggle([], []);
}, attrToggle, "must throw error to pass");
});
測試應通過,因爲錯誤拋出。