The main idea is start the program doing a test
- The Unit testing --> codigo que comprueba que otro código tiene que hacer lo que debe de hacer.
Ejemplos test:
function(sum) {
const msg = "hola"
return [a + b, msg]
}
--> Testing ex for check the code above.
typeof sum === function
sum(2, 5)[0] === 7
sum(2, 5)[1] === "hola"
Example the Unit testing. This example is very simple but illustrate several things about unit testings:
- The function is tested from several angles (we check what the function returns when passing different types of data to it)
- We compare the returned value with an expected value
- If these values match we show a green OK
- If these values doesn't match we show an error message to help us locate the problem
especializado en testear en vez del codigo como el TDD este se enfoca en testear partes de la app.
The most populars Jasmine (slide here), QUnit, Mocha, etc...
Now practice with Jasmine. Download Jasmine here and follow the instructions of teacher
Some matchers that Jasmine offer by default (we can build our own) are:
-
expect(x).toEqual(y);
compares objects or primitivesx
andy
and passes if they are equivalent -
expect(x).toBe(y);
compares objects or primitivesx
andy
and passes if they are the same object -
expect(x).toMatch(pattern);
comparesx
to string or regular expressionpattern
and passes if they match -
expect(x).toBeDefined();
passes ifx
is notundefined
-
expect(x).toBeUndefined();
passes ifx
isundefined
-
expect(x).toBeNull();
passes ifx
isnull
-
expect(x).toBeTruthy();
passes ifx
evaluates totrue
-
expect(x).toBeFalsy();
passes ifx
evaluates tofalse
-
expect(x).toContain(y);
passes if array or stringx
containsy
-
expect(x).toBeLessThan(y);
passes ifx
is less thany
-
expect(x).toBeGreaterThan(y);
passes ifx
is greater thany
-
expect(function(){fn();}).toThrow(e);
passes if functionfn
throws exceptione
when executed
For practice TDD in slide 62
For practice TDD in slide 63
Overcomplexity (overcomplexity programming)
For practice TDD in slide 64
Practice online here
This web for practice http://cyber-dojo.org/setup_default_start_point/show_exercises/?language=Javascript&test=jasmine