TESTING

function Assert

console.assert

Assert Function

function assert(expectedBehavior, descriptionOfCorrectBehavior) {




};

Parameters:

expectedBehavior : expression that evaluates to true or false (conditional)

descriptionOfCorrectBehavior : string value that explains what you expect your function to return

Assert Function

if (!expectedBehavior) {
  console.log(descriptionOfCorrectBehavior);
} 

if the expectedBehavior is not what you predict

(return false) then log the description of the correct behavior

!

'not' operator reverses the boolean value 

var hungry = true;
hungry // true
!hungry // false "not" hungry

Assert Function

else {
  console.log("test passed!")
}

if the expectedBehavior is what you predict

you log "test passed!"

Assert Function

function assert(expectedBehavior, descriptionOfCorrectBehavior) {





};

Parameters:

expectedBehavior : expression that evaluates to true or false (conditional)

descriptionOfCorrectBehavior : string value that explains what you expect your function to return

function assert(expectedBehavior, descriptionOfCorrectBehavior) {
  if (!expectedBehavior) {
    console.log(descriptionOfCorrectBehavior);
  }


};
function assert(expectedBehavior, descriptionOfCorrectBehavior) {
  if (!expectedBehavior) {
    console.log(descriptionOfCorrectBehavior);
  } else {
    console.log('test passed');
  }
};

Part II

Advanced Assert Function

function assert(expectedBehavior, descriptionOfCorrectBehavior) {
  if (!expectedBehavior) {
    console.log(descriptionOfCorrectBehavior || "Assertion failed");
  } else {
    console.log('test passed');
  }
};

if a descriptionOfCorrectBehavior argument is not passed into the function  invocation it will log "Assertion failed"

Advanced Assert Function

function assert(expectedBehavior, descriptionOfCorrectBehavior) {
  if (!expectedBehavior) {
    console.log(descriptionOfCorrectBehavior || "Assertion failed");
  } else {
    console.log('test passed');
  }
};
var addSquare = function(arr){
  var total = 0;
  each(arr, function(element){    
    total == element * element
  }
  return total;
};

var test = [1,2,3];
assert(addSquare(test) === 14)
var addSquare = function(arr){
  var total = 0;
  each(arr, function(element){    
    total == element * element
  }
  return total;
};

var test = [1,2,3];
assert(addSquare(test) === 14, "should return the sum of all the squared values")
var addSquare = function(arr){
  var total = 0;
  each(arr, function(element){    
    total += element * element
  }
  return total;
};

var test = [1,2,3];
assert(addSquare(test) === 14, "should return the sum of all the squared values")

what do you expect assert to log?

CONSOLE.ASSERT

console.assert(expression, object)

function greaterThan(a,b) {
  console.assert(a > b, {"message":"a is not greater than b","a":a,"b":b});
}
greaterThan(5,6);

Writes an error to the console when the evaluated expression is false.

function greaterThan(a,b) {
  console.assert(a > b, {"message":"a is not greater than b","a":a,"b":b});
}
greaterThan(10, 2);

if the evaluated expression is true console.assert won't return anything

function greaterThan(a,b) {
  console.assert(a > b, {"message":"a is not greater than b","a":a,"b":b});
}
greaterThan(10, 2);

//undefined

Assert

By telegraphprep

Assert

  • 644