Testing123
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
An introduction to testing and higher order functions.
Anonymous Functions Review
Introduction to Higher Order Functions
Testing
Coding Challenge
Overview
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Exercise Time!
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
https://github.com/TelegraphPrep/03-2016-calculator
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Functions PT3
Anonymous Functions & Higher Order Functions
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Anonymous Functions
function (num1, num2) {
return num1 + num2;
};
Creation
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Anonymous Functions
(function (num1, num2) {
return num1 + num2;
})();
Invocation: wrap your function in an IIFE (immediately invoked function EXPRESSION)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Anonymous Functions
(function (num1, num2) {
return num1 + num2;
})();
Usage: anonymous functions can only be called once
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Anonymous Functions
(function (num1, num2) {
return num1 + num2;
})();
Would this ever be helpful?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Higher Order Functions
Definition:
A function that takes another function
in as a PARAMETER
The function passed in as a parameter is called a CALLBACK
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Higher Order Functions
Example: loop
//loops through an array
//and invokes a callback on each element
var loop = function(arr, callback) {
for (var i = 0; i < arr.length; i++) {
callback(arr[i]);
}
};
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Higher Order Functions
Example: loop
var loop = function(arr, callback){
for (var i = 0; i < arr.length; i++){
callback(arr[i]);
}
};
// prints an element to the console
var print = function(element){
console.log(element);
};
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Higher Order Functions
Identifying the Callback
var arr = ['hi', 'my', 'name', 'is', 'albrey'];
loop(arr, print);
// hi
// my
// name
// is
// albrey
callback!
higher order function
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Higher Order Functions
Converting to an Anonymous Function
var arr = ['hi', 'my', 'name', 'is', 'albrey'];
loop(arr, function(element) {
console.log(element);
});
// hi
// my
// name
// is
// albrey
callback!
higher order function
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Higher Order Functions
So what's the point of all of this?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Testing
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Testing
-
Philosophy of Test Driven Development
-
`describe`, `it`, and `expect`, and the specrunner
-
Todays Sprint
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
TDD
Wikipedia says...
Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and ...
That's it for now.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
TDD: Development cycle
-
Write your test
-
Build your Function
-
Pass your test
-
Repeat
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Describe, it, expect
Describe, it, and expect are a part of the function library we'll be using today to build your tests.
Let's run through them together real quick!
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Describe, it, expect
Describe: Used to label the function we're testing.
describe('Subtract', function(){
});
First Parameter: a string
Second Parameter: an anonymous function
callback
higher order function
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Describe, it, expect
it: insert the definition of the function
it('it should take two numbers and return the difference', function(){
})
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Describe, it, expect
expect: test a `testcase` against what it's expected to be
it('it should take two numbers and return the difference', function(){
// create our test case
var subtractTestCase = calculator.subtract(5, 1);
// test it with expect
expect(subtractTestCase).to.be(4);
});
pass test case as argument to expecet
pass expected value as argument to `to.be` method
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Testing123
Today's sprint
-
Getting comfortable with anonymous functions
-
Getting comfortable with passing functions into functions.
-
Learning to read documentation (the read.me is god)
-
Have fun
https://github.com/telegraphprep/testing123
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Testing
Ruth's Copy of Testing123
By telegraphprep
Ruth's Copy of Testing123
An introduction to functional programming
- 652