SlideShare ist ein Scribd-Unternehmen logo
1 von 16
WEB09 - Unit testing
con JavaScript
Roberto Messora
roberto@messora.com - @robymes

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
Grazie a
Sponsor

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
Agenda
•

Prima dello Unit Testing: coding standards con JSLint

•

Javascript Unit Testing con Jasmine






•

Unit Testing in salsa BDD: Describe
Specifications
Expecations
Matchers
Spies

Automazione dello Unit Testing

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
Coding Standards
•

Prima ancora di pensare allo Unit Testing è necessario
pensare ad uno standard di scrittura del codice Javascript

•

JSLint non è sindacabile quando si tratta di scrivere codice
Javascript:
 Il nostro codice girerà su una serie di browser diversi che usano
interpreti diversi
 Il nostro codice molto probabilmente verrà minificato, le probabilità
che continui a funzionare aumentano se il sorgente è scritto
correttamente dal punto di vista formale

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
Framework di Unit Testing
•

Esistono diversi framework di Unit Testing Javascript





•

QUnit
Jasmine
Mocha
…

In questa sessione valuteremo Jasmine per la sua capacità
espressiva simile al BDD (Behavior Driven Design)

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
Specification
•

In Jasmine è possibile definire specification e raggrupparle tramite
le funzioni globali it e describe
describe("A suite", function() {
it("contains spec with expectations", function() {
//expectations
});
});

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
Expectations
•

In Jasmine è possibile definire le expectations tramite la funzione
globale expect abbinata ad una serie di matchers predefiniti
describe("A suite", function() {
it("contains spec with expectations", function() {
expect(true).toBe(true);
});
});

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
Setup e teardown
•

In Jasmine è possibile eseguire funzioni di setup e teardown prima e dopo ogni
singola specification utilizzando le funzioni globali beforeEach e afterEach
describe("A suite", function() {
beforeEach(function() {
//setup
});
afterEach(function() {
//teardown
});
it("contains spec with expectations", function() {
//expectations
});
});

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
demo
Una semplice suite di test

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
Test Runner
•

Eseguire una suite di test con Jasmine è abbastanza semplice, basta
una pagina HTML opportunamente configurata

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
demo
Test runner con Jasmine e Visual Studio

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
Spies
•

Le peculiarità di Javascript in quanto linguaggio in cui le funzioni
sono first class objects, permettono di creare spie e mock di
oggetti senza ricorrere a framework particolari come accade in altri
linguaggi
spyOn(myObj, "myObjMethod");
myObj.myObjMethod(2);
expect(myObj.myObjMethod).toHaveBeenCalled();
expect(myObj.myObjMethod).toHaveBeenCalledWith(2);

…

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
demo
Spies e mocks

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
Supporto per Async
•

Jasmine offre anche la possibilità di testare metodi asincroni
runs(function() {
//chiamata al metodo async
});
waitsFor(function() {
//poll finchè non ritorna true o va in timeout
}, “Error Message", 750);
runs(function() {
//expectations
});

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
Test Automation
• Automatizzare

il processo di esecuzione delle suite di Test
è possibile tramite ambienti di automazione come Karma
che permettono anche di:
 Eseguire le suite di Test su differenti browser tramite Phantom JS
 Integrare le suite di Test in ambienti di Continuous Integration

#CDays14 – Milano 25, 26 e 27 Febbraio 2014
Q&A
Tutto il materiale di questa sessione su

http://www.communitydays.it/
Lascia il feedback su questa sessione,
potrai essere estratto per i nostri premi!
Seguici su
Twitter @CommunityDaysIT
Facebook http://facebook.com/cdaysit
#CDays14

#CDays14 – Milano 25, 26 e 27 Febbraio 2014

Weitere ähnliche Inhalte

Ähnlich wie Javascript Unit Testing

Single Page Applications
Single Page ApplicationsSingle Page Applications
Single Page ApplicationsRoberto Messora
 
Sviluppare applicazioni web (completamente) on-line con Visual Studio Online ...
Sviluppare applicazioni web (completamente) on-line con Visual Studio Online ...Sviluppare applicazioni web (completamente) on-line con Visual Studio Online ...
Sviluppare applicazioni web (completamente) on-line con Visual Studio Online ...Marco Parenzan
 
CommunityDays 2015 - NPM, GRUNT E BOWER: IL NUOVO PACKAGE MANAGER DI VISUAL S...
CommunityDays 2015 - NPM, GRUNT E BOWER: IL NUOVO PACKAGE MANAGER DI VISUAL S...CommunityDays 2015 - NPM, GRUNT E BOWER: IL NUOVO PACKAGE MANAGER DI VISUAL S...
CommunityDays 2015 - NPM, GRUNT E BOWER: IL NUOVO PACKAGE MANAGER DI VISUAL S...Gianluca Carucci
 
Single Page web Application
Single Page web ApplicationSingle Page web Application
Single Page web ApplicationRoberto Messora
 
Da JavaScript a TypeScript
Da JavaScript a TypeScriptDa JavaScript a TypeScript
Da JavaScript a TypeScriptRoberto Messora
 
Doaw2020 - Dalla produzione alla QA, provisioning su SQL Server
Doaw2020 - Dalla produzione alla QA, provisioning su SQL ServerDoaw2020 - Dalla produzione alla QA, provisioning su SQL Server
Doaw2020 - Dalla produzione alla QA, provisioning su SQL ServerAlessandro Alpi
 
Fe04 angular js-101
Fe04   angular js-101Fe04   angular js-101
Fe04 angular js-101DotNetCampus
 
CDays15 - AZ08 - DocumentDB: il NoSql secondo Azure
CDays15 - AZ08 - DocumentDB: il NoSql secondo AzureCDays15 - AZ08 - DocumentDB: il NoSql secondo Azure
CDays15 - AZ08 - DocumentDB: il NoSql secondo AzureDavide Benvegnù
 
ASP.NET 4.6 e ASP.NET 5...l'evoluzione del web
ASP.NET 4.6 e ASP.NET 5...l'evoluzione del webASP.NET 4.6 e ASP.NET 5...l'evoluzione del web
ASP.NET 4.6 e ASP.NET 5...l'evoluzione del webAndrea Dottor
 
Vs04 - DevOps: Continuous Deployment con Windows Azure
Vs04 - DevOps: Continuous Deployment con Windows AzureVs04 - DevOps: Continuous Deployment con Windows Azure
Vs04 - DevOps: Continuous Deployment con Windows AzureAntonio Liccardi
 
Introduzione a JavaScript e jQuery (1/2)
Introduzione a JavaScript e jQuery (1/2)Introduzione a JavaScript e jQuery (1/2)
Introduzione a JavaScript e jQuery (1/2)Giuseppe Vizzari
 
[ITA] Sql Saturday 355 in Parma - New SQL Server databases under source control
[ITA] Sql Saturday 355 in Parma - New SQL Server databases under source control[ITA] Sql Saturday 355 in Parma - New SQL Server databases under source control
[ITA] Sql Saturday 355 in Parma - New SQL Server databases under source controlAlessandro Alpi
 
ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)Giorgio Di Nardo
 
Real world Visual Studio Code
Real world Visual Studio CodeReal world Visual Studio Code
Real world Visual Studio CodeRoberto Messora
 
Community Days 2015 Introduzione a Xamarin
Community Days 2015  Introduzione a XamarinCommunity Days 2015  Introduzione a Xamarin
Community Days 2015 Introduzione a XamarinDan Ardelean
 
Introduzione a TypeScript
Introduzione a TypeScriptIntroduzione a TypeScript
Introduzione a TypeScriptSinergia Totale
 
TYPESCRIPT, ANGULAR E BOOTSTRAP ASSIEME PER APPLICAZIONI REAL WORLD
TYPESCRIPT, ANGULAR E BOOTSTRAP ASSIEME PER APPLICAZIONI REAL WORLDTYPESCRIPT, ANGULAR E BOOTSTRAP ASSIEME PER APPLICAZIONI REAL WORLD
TYPESCRIPT, ANGULAR E BOOTSTRAP ASSIEME PER APPLICAZIONI REAL WORLDDotNetCampus
 

Ähnlich wie Javascript Unit Testing (20)

Single Page Applications
Single Page ApplicationsSingle Page Applications
Single Page Applications
 
Sviluppare applicazioni web (completamente) on-line con Visual Studio Online ...
Sviluppare applicazioni web (completamente) on-line con Visual Studio Online ...Sviluppare applicazioni web (completamente) on-line con Visual Studio Online ...
Sviluppare applicazioni web (completamente) on-line con Visual Studio Online ...
 
CommunityDays 2015 - NPM, GRUNT E BOWER: IL NUOVO PACKAGE MANAGER DI VISUAL S...
CommunityDays 2015 - NPM, GRUNT E BOWER: IL NUOVO PACKAGE MANAGER DI VISUAL S...CommunityDays 2015 - NPM, GRUNT E BOWER: IL NUOVO PACKAGE MANAGER DI VISUAL S...
CommunityDays 2015 - NPM, GRUNT E BOWER: IL NUOVO PACKAGE MANAGER DI VISUAL S...
 
Single Page web Application
Single Page web ApplicationSingle Page web Application
Single Page web Application
 
Sviluppare SPA con Vue
Sviluppare SPA con VueSviluppare SPA con Vue
Sviluppare SPA con Vue
 
Sviluppare SPA con Vue.js
Sviluppare SPA con Vue.jsSviluppare SPA con Vue.js
Sviluppare SPA con Vue.js
 
Da JavaScript a TypeScript
Da JavaScript a TypeScriptDa JavaScript a TypeScript
Da JavaScript a TypeScript
 
Doaw2020 - Dalla produzione alla QA, provisioning su SQL Server
Doaw2020 - Dalla produzione alla QA, provisioning su SQL ServerDoaw2020 - Dalla produzione alla QA, provisioning su SQL Server
Doaw2020 - Dalla produzione alla QA, provisioning su SQL Server
 
Fe04 angular js-101
Fe04   angular js-101Fe04   angular js-101
Fe04 angular js-101
 
CDays15 - AZ08 - DocumentDB: il NoSql secondo Azure
CDays15 - AZ08 - DocumentDB: il NoSql secondo AzureCDays15 - AZ08 - DocumentDB: il NoSql secondo Azure
CDays15 - AZ08 - DocumentDB: il NoSql secondo Azure
 
VS Package @ CD2008
VS Package @ CD2008VS Package @ CD2008
VS Package @ CD2008
 
ASP.NET 4.6 e ASP.NET 5...l'evoluzione del web
ASP.NET 4.6 e ASP.NET 5...l'evoluzione del webASP.NET 4.6 e ASP.NET 5...l'evoluzione del web
ASP.NET 4.6 e ASP.NET 5...l'evoluzione del web
 
Vs04 - DevOps: Continuous Deployment con Windows Azure
Vs04 - DevOps: Continuous Deployment con Windows AzureVs04 - DevOps: Continuous Deployment con Windows Azure
Vs04 - DevOps: Continuous Deployment con Windows Azure
 
Introduzione a JavaScript e jQuery (1/2)
Introduzione a JavaScript e jQuery (1/2)Introduzione a JavaScript e jQuery (1/2)
Introduzione a JavaScript e jQuery (1/2)
 
[ITA] Sql Saturday 355 in Parma - New SQL Server databases under source control
[ITA] Sql Saturday 355 in Parma - New SQL Server databases under source control[ITA] Sql Saturday 355 in Parma - New SQL Server databases under source control
[ITA] Sql Saturday 355 in Parma - New SQL Server databases under source control
 
ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)
 
Real world Visual Studio Code
Real world Visual Studio CodeReal world Visual Studio Code
Real world Visual Studio Code
 
Community Days 2015 Introduzione a Xamarin
Community Days 2015  Introduzione a XamarinCommunity Days 2015  Introduzione a Xamarin
Community Days 2015 Introduzione a Xamarin
 
Introduzione a TypeScript
Introduzione a TypeScriptIntroduzione a TypeScript
Introduzione a TypeScript
 
TYPESCRIPT, ANGULAR E BOOTSTRAP ASSIEME PER APPLICAZIONI REAL WORLD
TYPESCRIPT, ANGULAR E BOOTSTRAP ASSIEME PER APPLICAZIONI REAL WORLDTYPESCRIPT, ANGULAR E BOOTSTRAP ASSIEME PER APPLICAZIONI REAL WORLD
TYPESCRIPT, ANGULAR E BOOTSTRAP ASSIEME PER APPLICAZIONI REAL WORLD
 

Mehr von Roberto Messora

Azure Synapse: data lake & modern data warehouse dalla A alla Z
Azure Synapse: data lake &  modern data warehouse dalla A alla ZAzure Synapse: data lake &  modern data warehouse dalla A alla Z
Azure Synapse: data lake & modern data warehouse dalla A alla ZRoberto Messora
 
Azure Data Factory: l'evoluzione della specie della data integration
Azure Data Factory: l'evoluzione della specie della data integrationAzure Data Factory: l'evoluzione della specie della data integration
Azure Data Factory: l'evoluzione della specie della data integrationRoberto Messora
 
Architetture a Microservizi con Docker Container
Architetture a Microservizi con Docker ContainerArchitetture a Microservizi con Docker Container
Architetture a Microservizi con Docker ContainerRoberto Messora
 
Docker as a hosting target
Docker as a hosting targetDocker as a hosting target
Docker as a hosting targetRoberto Messora
 
Da imperativo a reattivo: Bacon.JS
Da imperativo a reattivo: Bacon.JSDa imperativo a reattivo: Bacon.JS
Da imperativo a reattivo: Bacon.JSRoberto Messora
 
Event streaming pipeline with Windows Azure and ArcGIS Geoevent extension
Event streaming pipeline with Windows Azure and ArcGIS Geoevent extensionEvent streaming pipeline with Windows Azure and ArcGIS Geoevent extension
Event streaming pipeline with Windows Azure and ArcGIS Geoevent extensionRoberto Messora
 
Javascript avanzato: sfruttare al massimo il web
Javascript avanzato: sfruttare al massimo il webJavascript avanzato: sfruttare al massimo il web
Javascript avanzato: sfruttare al massimo il webRoberto Messora
 
Self-adaptive geospatial web applications
Self-adaptive geospatial web applicationsSelf-adaptive geospatial web applications
Self-adaptive geospatial web applicationsRoberto Messora
 
Web technologies and patterns in HTML5 mapping
Web technologies and patterns in HTML5 mappingWeb technologies and patterns in HTML5 mapping
Web technologies and patterns in HTML5 mappingRoberto Messora
 
MV* presentation frameworks in Javascript: en garde, pret, allez!
MV* presentation frameworks in Javascript: en garde, pret, allez!MV* presentation frameworks in Javascript: en garde, pret, allez!
MV* presentation frameworks in Javascript: en garde, pret, allez!Roberto Messora
 

Mehr von Roberto Messora (13)

Azure Synapse: data lake & modern data warehouse dalla A alla Z
Azure Synapse: data lake &  modern data warehouse dalla A alla ZAzure Synapse: data lake &  modern data warehouse dalla A alla Z
Azure Synapse: data lake & modern data warehouse dalla A alla Z
 
Azure Data Factory: l'evoluzione della specie della data integration
Azure Data Factory: l'evoluzione della specie della data integrationAzure Data Factory: l'evoluzione della specie della data integration
Azure Data Factory: l'evoluzione della specie della data integration
 
Introduzione a Docker
Introduzione a DockerIntroduzione a Docker
Introduzione a Docker
 
Seminario Big Data
Seminario Big DataSeminario Big Data
Seminario Big Data
 
Architetture a Microservizi con Docker Container
Architetture a Microservizi con Docker ContainerArchitetture a Microservizi con Docker Container
Architetture a Microservizi con Docker Container
 
Docker as a hosting target
Docker as a hosting targetDocker as a hosting target
Docker as a hosting target
 
Da imperativo a reattivo: Bacon.JS
Da imperativo a reattivo: Bacon.JSDa imperativo a reattivo: Bacon.JS
Da imperativo a reattivo: Bacon.JS
 
Event streaming pipeline with Windows Azure and ArcGIS Geoevent extension
Event streaming pipeline with Windows Azure and ArcGIS Geoevent extensionEvent streaming pipeline with Windows Azure and ArcGIS Geoevent extension
Event streaming pipeline with Windows Azure and ArcGIS Geoevent extension
 
Javascript avanzato: sfruttare al massimo il web
Javascript avanzato: sfruttare al massimo il webJavascript avanzato: sfruttare al massimo il web
Javascript avanzato: sfruttare al massimo il web
 
Self-adaptive geospatial web applications
Self-adaptive geospatial web applicationsSelf-adaptive geospatial web applications
Self-adaptive geospatial web applications
 
Web technologies and patterns in HTML5 mapping
Web technologies and patterns in HTML5 mappingWeb technologies and patterns in HTML5 mapping
Web technologies and patterns in HTML5 mapping
 
Usare Knockout JS
Usare Knockout JSUsare Knockout JS
Usare Knockout JS
 
MV* presentation frameworks in Javascript: en garde, pret, allez!
MV* presentation frameworks in Javascript: en garde, pret, allez!MV* presentation frameworks in Javascript: en garde, pret, allez!
MV* presentation frameworks in Javascript: en garde, pret, allez!
 

Javascript Unit Testing

  • 1. WEB09 - Unit testing con JavaScript Roberto Messora roberto@messora.com - @robymes #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 2. Grazie a Sponsor #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 3. Agenda • Prima dello Unit Testing: coding standards con JSLint • Javascript Unit Testing con Jasmine      • Unit Testing in salsa BDD: Describe Specifications Expecations Matchers Spies Automazione dello Unit Testing #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 4. Coding Standards • Prima ancora di pensare allo Unit Testing è necessario pensare ad uno standard di scrittura del codice Javascript • JSLint non è sindacabile quando si tratta di scrivere codice Javascript:  Il nostro codice girerà su una serie di browser diversi che usano interpreti diversi  Il nostro codice molto probabilmente verrà minificato, le probabilità che continui a funzionare aumentano se il sorgente è scritto correttamente dal punto di vista formale #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 5. Framework di Unit Testing • Esistono diversi framework di Unit Testing Javascript     • QUnit Jasmine Mocha … In questa sessione valuteremo Jasmine per la sua capacità espressiva simile al BDD (Behavior Driven Design) #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 6. Specification • In Jasmine è possibile definire specification e raggrupparle tramite le funzioni globali it e describe describe("A suite", function() { it("contains spec with expectations", function() { //expectations }); }); #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 7. Expectations • In Jasmine è possibile definire le expectations tramite la funzione globale expect abbinata ad una serie di matchers predefiniti describe("A suite", function() { it("contains spec with expectations", function() { expect(true).toBe(true); }); }); #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 8. Setup e teardown • In Jasmine è possibile eseguire funzioni di setup e teardown prima e dopo ogni singola specification utilizzando le funzioni globali beforeEach e afterEach describe("A suite", function() { beforeEach(function() { //setup }); afterEach(function() { //teardown }); it("contains spec with expectations", function() { //expectations }); }); #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 9. demo Una semplice suite di test #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 10. Test Runner • Eseguire una suite di test con Jasmine è abbastanza semplice, basta una pagina HTML opportunamente configurata #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 11. demo Test runner con Jasmine e Visual Studio #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 12. Spies • Le peculiarità di Javascript in quanto linguaggio in cui le funzioni sono first class objects, permettono di creare spie e mock di oggetti senza ricorrere a framework particolari come accade in altri linguaggi spyOn(myObj, "myObjMethod"); myObj.myObjMethod(2); expect(myObj.myObjMethod).toHaveBeenCalled(); expect(myObj.myObjMethod).toHaveBeenCalledWith(2); … #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 13. demo Spies e mocks #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 14. Supporto per Async • Jasmine offre anche la possibilità di testare metodi asincroni runs(function() { //chiamata al metodo async }); waitsFor(function() { //poll finchè non ritorna true o va in timeout }, “Error Message", 750); runs(function() { //expectations }); #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 15. Test Automation • Automatizzare il processo di esecuzione delle suite di Test è possibile tramite ambienti di automazione come Karma che permettono anche di:  Eseguire le suite di Test su differenti browser tramite Phantom JS  Integrare le suite di Test in ambienti di Continuous Integration #CDays14 – Milano 25, 26 e 27 Febbraio 2014
  • 16. Q&A Tutto il materiale di questa sessione su http://www.communitydays.it/ Lascia il feedback su questa sessione, potrai essere estratto per i nostri premi! Seguici su Twitter @CommunityDaysIT Facebook http://facebook.com/cdaysit #CDays14 #CDays14 – Milano 25, 26 e 27 Febbraio 2014

Hinweis der Redaktion

  1. Slide da mostrare prima di iniziare la sessione – non rimuovere!
  2. Ultima slide, obbligatoria