24
Unit testing Thomas Dunajski gfnork webentwicklungsunternehmen Assert, Mocha

node.js workshop- node.js unit testing

Tags:

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: node.js workshop- node.js unit testing

Unit testing

Thomas Dunajskigfnork webentwicklungsunternehmen

Assert, Mocha

Page 2: node.js workshop- node.js unit testing

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

Page 3: node.js workshop- node.js unit testing

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

Page 4: node.js workshop- node.js unit testing

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

Page 5: node.js workshop- node.js unit testing

5

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

Page 6: node.js workshop- node.js unit testing

6

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

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

import

Compaing

Page 7: node.js workshop- node.js unit testing

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

Page 8: node.js workshop- node.js unit testing

8

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

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

Page 9: node.js workshop- node.js unit testing

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

Page 10: node.js workshop- node.js unit testing

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)

Page 11: node.js workshop- node.js unit testing

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

Page 12: node.js workshop- node.js unit testing

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

Page 13: node.js workshop- node.js unit testing

13

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

Page 14: node.js workshop- node.js unit testing

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

Page 15: node.js workshop- node.js unit testing

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

Page 16: node.js workshop- node.js unit testing

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

Page 17: node.js workshop- node.js unit testing

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

Page 18: node.js workshop- node.js unit testing

18

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

are grouped within

'something'

Page 19: node.js workshop- node.js unit testing

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

Page 20: node.js workshop- node.js unit testing

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

Page 21: node.js workshop- node.js unit testing

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

Page 22: node.js workshop- node.js unit testing

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

Page 23: node.js workshop- node.js unit testing

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.

Page 24: node.js workshop- node.js unit testing

24

Questions?