我經常手動測試JavaScript函數的輸出(通過簡單地查看控制檯中每個函數的輸出),而這通常會非常繁瑣。在JavaScript中,是否有任何方法可以自動測試一系列函數調用的輸出,並返回所有不會產生預期結果的測試?自動測試JavaScript函數的輸出
checkOutput([["add(1, 2)", 3], ["add(2, 2)", 4]]); //if the input does not match the output in one of these arrays, then return the function call(s) that didn't produce the correct output
function checkOutput(functionArray){
//this function is not yet implemented, and should return a list of function calls that did not produce correct output (if there are any).
}
function add(num1, num2){
return num1 + num2;
}
有一個在此行缺少括號:'回報的eval(E [0])== E [1]);' –
感謝您指出了這一點 - 我原本寫它作爲一個'if',然後意識到我!在寫'如果(東西)返回true,否則返回FALSE' –
這裏是的jsfiddle此功能的測試:http://jsfiddle.net/jarble/xZ5uy/ –