Test innode

316
-1

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
316
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Test innode

  1. 1. NodeJs testing tools and continuous integration Davide Fiorello
  2. 2. Mocha • Javascript Test Framework • Run in Node.js and Browser environment • BDD, TDD, QUnit • Synchronous and Asynchronous
  3. 3. Mocha - Interfaces • BDD 1 describe('My first test', function(){ 2 it('pass it!!'); 3 }); • TDD 1 suite('Array', function(){ 2 setup(function(){ 3 }); 4 5 suite('#indexOf()', function(){ 6 test('should return -1 when not present', function(){ 7 // ASSERT 8 }); 9 }); 10 });
  4. 4. Mocha - Sync, Async, Pending 1 describe('Let's test..', function() { 2 it('Test something sync', function() { 3 // ASSERT 4 }); 5 6 it('Test something async', function(done) { 7 user.save(function(err){ 8 if (err) throw err; 9 // ASSERT 10 done(); 11 }); 12 }); 13 14 it('Pending test'); 15 });
  5. 5. Mocha - Before, After, ... 1 describe('Let's test..', function() { 2 before(function(){ 3 // ... 4 }); 5 6 beforeEach(function(done){ 7 // ... -> done() 8 }); 9 10 after(function(){ 11 // ... 12 }); 13 14 afterEach(function(){ 15 // ... 16 }); 17 });
  6. 6. Mocha - Only, skip 1 describe('A test', function() { 2 it.only('Test only this', function(done) { 3 // ... 4 }); 5 }); 6 7 describe('Another test', function() { 8 it.skip('Don't test this', function(done) { 9 // ... 10 }); 11 });
  7. 7. Mocha - Has not... • Assert (should.js, chai, expect.js) • Spy/Mock/Stub (sinon) • Code Coverage (istanbul, node- jscoverage)
  8. 8. Should.js should is an expressive, readable, test framework agnostic, assertion library. Main goals of this library to be expressive and to be helpful. It keeps your test code clean, and your error messages helpful.
  9. 9. Should.js 1 var should = require('should'); 2 3 var user = { name: 'davide', pets: ['tobi', 'loki']}; 4 5 user.should.have.property('name', 'davide'); 6 user.should.have.property('pets').with.lengthOf(2); 7 8 should(user).have.property('name', 'davide'); 9 should(true).ok; 10 11 should.exist(user); It extends the Object.prototype with a single non- enumerable getter that allows you to express how that object should behave, also it returns itself when required with require.
  10. 10. Should.js - Chaining assertions .ok, .true, .false, .eql, .equal, .startWith, .endWith, .within, .above, .belove, .instanceOf, .Array, .Object, .String, .Error, .property, .length, .keys, .match, .throw, .throwError, .json, .html 1 var should = require('should'); 2 3 var user = { name: 'davide', age : 22, pets : ['pit', 'fuffy']}; 4 5 user.name.should.match(/dav/); 6 user.age.should.be.above(18); 7 user.pets.should.be.Array; 8 user.pets.should.containEql('pit'); 9 user.pets.should.not.containEql('mark');
  11. 11. Should.js - Chaining assertions 1 var should = require('should'); 2 3 var user = { name: 'davide', age : 22}; 4 5 user.age.should.be.greaterThan(18).and.be.lessThan(33); 6 7 user.age.should.greaterThan(18).lessThan(33); .an, .of, .a,.and, .be, .have, .with, .is, .which Use them for better readability; they do nothing at all
  12. 12. Sinon.js Standalone test spies, stubs and mocks for JavaScript.No dependencies, works with any unit testing framework.
  13. 13. Sinon.js • Spies • Stubs • Mocks • Fake timers • Fake XHR • Fake server • Sandboxing • Assertions • Matchers
  14. 14. Sinon.js - spies 1 var sinon = require('sinon'); 2 var should = require('should'); 3 4 function myFunc(value) { 5 myObject.check(value); 6 } 7 8 var myObject = { 9 check : function(value) { 10 // DO Something 11 } 12 }; 13 var spy = sinon.spy(myObject, "check"); 14 myFunc('test'); 15 spy.called.should.be.true;
  15. 15. Sinon.js - spies (API) .withArgs, .callCount, .called, .calledOnce, .firstCall, .lastCall, .calledBefore, .calledAfter, .calledWith, .alwaysCalledWith, .neverCalledWith, .threw, .returned, .args, .returnValues
  16. 16. Sinon.js - stubs 1 var sinon = require('sinon'); 2 var should = require('should'); 3 4 function myFunc() { 5 return myObject.check(); 6 } 7 8 var myObject = { 9 check : function() { 10 return 10; 11 // DO Something 12 } 13 }; 14 var stub = sinon.stub(myObject, "check"); 15 stub.onFirstCall().returns(1) 16 .onSecondCall().returns(2); 17 18 myFunc('test').should.be.equal(1); 19 myFunc('test').should.be.equal(2);
  17. 17. Sinon.js - Stub (API) .withArgs, .onCall, .onFirstCall, .onSecondCall, .returns, .returnsThis, .returnsArg, .throws, .callsArg, .yelds, .callArgWith, .callArgAsync
  18. 18. Useful Modules • node-mocks-http • node-rest-client • pow-mongodb-fixtures • readyness
  19. 19. Continuous Integration • drone.io • travis-ci.io • codeship.io
  20. 20. Thanks!! Davide Fiorello davide@codeflyer.com github.com/codeflyer

×