Testing123

An introduction to testing and higher order functions.

 

Anonymous Functions Review

Introduction to Higher Order Functions

Testing

Coding Challenge

Overview

Exercise Time!

https://github.com/TelegraphPrep/03-2016-calculator

Functions PT3

Anonymous Functions & Higher Order Functions

Anonymous Functions

function (num1, num2){
    return num1 + num2;
};

Creation

Anonymous Functions

(function (num1, num2){
    return num1 + num2;
})(2, 2)
//4

Invocation: wrap your function in an IIFI (immediately invoked function invocation)

Anonymous Functions

(function (num1, num2){
    return num1 + num2;
})(2, 2)
//4

Usage: anonymous functions can only be called once

Anonymous Functions

(function (num1, num2){
    return num1 + num2;
})(2, 2)
//4

Would this ever be helpful?

Higher Order Functions

Definition:

A function that takes another function as a callback

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]);
    }
};

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);
};

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

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

Higher Order Functions

So what's the point of all of this?

Testing

Testing

  1. Philosophy of Test Driven Development

  2. `describe`, `it`, and `expect`, and the specrunner

  3. Todays Sprint

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.

TDD: Development cycle

  1. Write your test

  2. Build your Function

  3. Pass your test

  4. Repeat

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!

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

Describe, it, expect

it: insert the definition of the function

it('it should take two numbers and return the difference', function(){



})

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

Testing123

Today's sprint

  1. Getting comfortable with anonymous functions

  2. Getting comfortable with passing functions into functions.

  3. Learning to read documentation (the read.me is god)

  4. Have fun

https://github.com/telegraphprep/testing123

Testing

Higher Order Function Inception

By telegraphprep

Higher Order Function Inception

Building higher order functions with higher order functions.

  • 934