node.js workshop- node.js unit testing

Tags:

Preview:

DESCRIPTION

 

Citation preview

Unit testing

Thomas Dunajskigfnork webentwicklungsunternehmen

Assert, Mocha

2

Unit Testing“Unit testing is a lot like going to the gym. You know it is good for you, all the arguments make sense, so you start working out. There's an initial rush, which is great, but after a few days you start to wonder if it is worth the trouble.” - some guy on Stack Overflow

3

What is Unit Testing?make sure your project worksuse control data with deterministic resultcompare the expected result of a Unit with the actualtest should be short and test only one thing at a timein functional programing a Unit usually is a single functionin OOP it can be a class or just a method

4

Why is testing important?helps you understand what the code should really dohelps finding Bugsmakes sure you dont break existing Codeproves your code works so you can sell itensures code quality

5

What is AAA?Arange prepare the unit for the testActexecute the function you want to testAssertcompare the reult

6

The Assert Modulvar assert = require('assert');

var actual = 23,expected = 42;assert.equal(actual, expected);

import

Compaing

7

The should Modulrequire('should');

var actual = 23,expected = 42;actual.should.equal(expected);

Diffrent syntax but does the same Thing

Expends the Prototype of Object

8

The AssertThat Modulevar assert = require('node-assertthat');

var actual = 23,expected = 42;assert.that(actual, is.equalTo(expected));

9

What is the Difference?assert is technically oriented tests the Implementationshould is semantic orientedtests the Behaviour moves the focus from the test to the componentassertthat is technically oriented like assert but wants to be readeble like should mixture of the above modules

10

Witch one is Better?no correct answer all work just choose the one u like moreevrything that follows works with all 3 modules(I prefer should as it is easy to read)

11

Thats all you need...function sum(a,b){ return a+b;}expected = 1337;var actual = sum(1000 , 337), actual.should.equal(expected);

...but it gets better

ArangeAct

Assert

12

What is Mocha?Mocha is a test envoirmentit runs your tests and analizes the resultsit adds comfort and saves timecan test server and clientside

13

TDD or BDD?Test Driven DevelopmentBehavior Driven Developmentjust diffrent naming conventionsTDD assert BDD should

14

TDD with AssertThatvar assert = require('node-assertthat');

test('should be different', function () { var actual = 23, expected = 42; assert.that(actual, is.not.equalTo(expected));});

15

BDD with Should

it('should return -1 if item is not contained', function () { var actual = [1, 2, 3].indexOf(4), expected = -1; actual.should.equal(expected); });

returns -1 if not

contained

no require needed

16

Why is there no require?mocha can be configurated to do things for youjusts wirte a mocha.opts file and put it with your tests in a folder called testwhen you execute mocha it will open that file and apply the options before executing all test in the folder <--reqire('should')> in mocha.opts automatiaclly imports the module in your tests (does not work with assert as it has to be assigned)if you want to use TDD write <--ui tdd> in your mocha.opts file

17

Structuring Testsdescribe('#indexOf()', function () { it('should return -1 if item is not contained', function () { var actual = [1, 2, 3].indexOf(4), expected = -1; actual.should.equal(expected); });

Suite in TDD

18

Structuring Testsdescribe('something', function () { describe('something else', function () { //some test }); describe('some more', function () { //some sother test });});

are grouped within

'something'

19

How to prepare Tests?before(function(done){ console.log('before') done();});

beforeEach(function(done){ console.log('beforeEach') done();});

Executed once before

the tests start

Executed once before

each test

20

How to test Asynchronous Functions?this.timeout(3000); it('should run async!', function(done){ setTimeout(function(){ var actual = 1+1, expected = 2; actual.should.equal(expected); done(); }, 2500); });

tells the test that its async

delays the function to

make it async

sets the maximum time a test can run before it is caneled

21

Exceptions describe('Exceptions', function(){ it('should throw a "fail" Exception', function(){ (function(){ throw new Error('fail'); }).should.throw('fail'); });

it('should not throw a Exception', function(){ (function(){ //do nothing }).should.not.throw(); }); });

Throwing Exception

22

How to improve Mocha's output?default output in Mocha is just one '.' for each test<--reporter list> creates readable output <--reporter spec> creates readable output that shows your test hirarchy <--reporter doc> creates HTML

23

LivecodingWe will write a test, that tests the Redis example code used in the Database Section of the Workshop. Notice that the example code runs async and we need to prepare Redis in advance. We will also split the code into two JS files.

24

Questions?