2017-07-03 175 views
0

我想在這裏創建一個assertTesting函數,它比較實際值和期望值與嚴格相等(不是類型轉換)。assertTesting函數成功或失敗測試

此處作爲一例是一個成功的案例:

function multiplyByTwo(n) { 
    return n * 2; 
} 
var output = multiplyByTwo(2); // returns 4 
assertTesting(output, 4, 'it doubles 2 to 4'); 
// console output: 
// passed 

這裏是一個失敗的一個:

function multiplyByTwo(n) { 
    return (n * 2) + 1; // an incorrect implementation 
} 
var output = multiplyByTwo(2); // returns 5 
assertTesting(output, 4, 'it doubles 2 to 4'); 
// console output: 
// FAILED [it doubles 2 to 4] Expected "4", but got "5" 

這裏是到目前爲止我的代碼:

var output; 

function multiplyByTwo(n) { 
    output = n * 2; 
    return output; 
} 

function assertTesting (actual, expected, testName) { 
    if(actual === ouput && expected === testName){ 
    return "SUCCESS" + [testname] + "Expected" + expected + "," "but got" + actual; 
    } 
} 

任何想法我如何能得到我需要的確切輸出?請幫忙。

回答

1

應該是這樣的,我想:

function assertEqual(actual, expected, testName) { 
    if(actual !== expected){ 
    console.error("FAIL [" + testname + "] Expected \"" + expected + ", \" but got \"" + actual + "\""); 
    } else { 
    console.info("SUCCESS [" + testname + "]"); 
    } 
} 
+0

怎樣的成功?那麼它會如何呢? –

+0

你想成功嗎? 'passed'? – dloeda

+0

正確。通過的一個。 –