T.D.D.

Antes de empezar

¿Quién tiene o ha tenido miedo de tocar el código?

¿Quién tiene confianza de hacer un deploy sin romper nada?

¿Por qué TDD?

Las objeciones más comunes

"Escribir pruebas unitarias toma demasiado tiempo"

"¿Cómo puedo escribir primero las pruebas si no sé lo que hace eso, todavía?"

"Las pruebas unitarias no detectarán todos los errores"

Hay que darle una oportunidad

Me fuerza a pensar

Hace la depuración más fácil

Hace que el programar se disfrute más

¿Qué es TDD?

Siempre falla

function add(x, y) {
    return x + y;
}

add(2, 3);      //5

add(2, 3);      //6

add('2', '3');  //'23'

¿Cuáles son las expectativas?

Expectativas

  • 2 + 3 = 5
  • Error si no se usan números
  • 0.1 + 0.2 = 0.3

2 + 3 = 5

Error si no se usan números

0.1 + 0.2 = 0.3

function add(x, y) {
    return x + y;
}

2 + 3 = 5

Error si no se usan números

0.1 + 0.2 = 0.3

function add(x, y) {
    if((typeof x && typeof y) !== 'number'){
        throw new Error('Params must be a number.')
    }

    return x + y;
}

2 + 3 = 5

Error si no se usan números

0.1 + 0.2 = 0.3

function add(x, y) {
    var result;
    
    if((typeof x && typeof y) !== 'number'){
        throw new Error('Params must be a number.')
    }

    result = x + y;

    if (parseInt(result) !== result) {
        result = parseFloat(result.toFixed(1));
    }

    return result;
}
function add(x, y) {
    var result;
    if((typeof x && typeof y) !== 'number'){
        throw new Error('Params must be a number.')
    }
    result = x + y;

    if (parseInt(result) !== result) {
        result = parseFloat(result.toFixed(1));
    }

    return result;
}


//Expect add(2, 3) to equal 5

//Expect add() to throw an error if x/y are not numbers

//Expect add(0.1, 0.2) to equal 0.3
function add(x, y) {
    var result;
    if((typeof x && typeof y) !== 'number'){
        throw new Error('Params must be a number.')
    }
    result = x + y;

    if (parseInt(result) !== result) {
        result = parseFloat(result.toFixed(1));
    }

    return result;
}


//Expect add(2, 3) to equal 5
expect(add(2, 3)).toBe(5);

//Expect add() to throw an error if x/y are not numbers

//Expect add(0.1, 0.2) to equal 0.3
function add(x, y) {
    var result;
    if((typeof x && typeof y) !== 'number'){
        throw new Error('Params must be a number.')
    }
    result = x + y;

    if (parseInt(result) !== result) {
        result = parseFloat(result.toFixed(1));
    }

    return result;
}


//Expect add(2, 3) to equal 5
expect(add(2, 3)).toBe(5);

//Expect add() to throw an error if x/y are not numbers
expect(add(2, 'test')).toThrow();

//Expect add(0.1, 0.2) to equal 0.3
function add(x, y) {
    var result;
    if((typeof x && typeof y) !== 'number'){
        throw new Error('Params must be a number.')
    }
    result = x + y;

    if (parseInt(result) !== result) {
        result = parseFloat(result.toFixed(1));
    }

    return result;
}


//Expect add(2, 3) to equal 5
expect(add(2, 3)).toBe(5);

//Expect add() to throw an error if x/y are not numbers
expect(add(2, 'test')).toThrow();

//Expect add(0.1, 0.2) to equal 0.3
expect(add(0.1, 0.2)).toBe(0.3);
Made with Slides.com