TESTING
function Assert
console.assert
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Assert Function
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
else {
console.log("test passed!")
}
if the expectedBehavior is what you predict
you log "test passed!"
Assert Function
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Advanced Assert Function
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
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
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
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.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/3109893/Screen_Shot_2016-10-12_at_2.36.27_PM.png)
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