lundi 25 novembre 2019

javascript simple assertEqual test run result

I am learning JS and this is something I could not understand even after watching tutorials online.

the function assertEqual compare actual and expected values with strict equality, then log one of two messages to the console.

function assertEqual(actual, expected, testName) {
    if (actual === expected) {
        console.log("passed")
    } else {
        console.log('FAILED [' + testName + '] Expected "' + expected + '", but got "' + actual + '"')
    }
}

The test case is supposed to return passed and FAILED respectively.

function multiplyByTwo(n) {
    return n * 2;
}
var output = multiplyByTwo(2); // returns 4
assertEqual(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
assertEqual(output, 4, 'it doubles 2 to 4');
// console output:
// FAILED [it doubles 2 to 4] Expected "4", but got "5"

however, the results are both failed...

FAILED [it doubles 2 to 4] Expected "4", but got "5"
FAILED [it doubles 2 to 4] Expected "4", but got "5"

If I change the test case, however, with variable names like below...

let actualOne = multiplyByTwo(2)
let expectedOne = 4;
let testNameOne = 'it doubles 2 to 4'

assertEqual(actualOne, expectedOne, testNameOne) // passed

let actualTwo = multiplyByTwo(2)
let expectedTwo = 5;
let testNameTwo = 'it doubles 2 to 4'

assertEqual(actualTwo, expectedTwo, testNameTwo)

these returns

FAILED [it doubles 2 to 4] Expected "4", but got "5"
passed

So my question is, why does the first trial not work? What am I missing?

Aucun commentaire:

Enregistrer un commentaire