SlideShare a Scribd company logo
1 of 13
mocking Emanuele DelBono  emanuele@codiceplastico.com http://blog.codiceplastico.com
Agenda
Perchè? Se uno unit test è rossol’errore è sicuramentenell’unità sotto test
Definizioni State Based Testing: verifica che un unità funzioni correttamente verificandone lo stato dopo l’esecuzione Interaction Testing: verifica che l’unità sotto test effettui correttamente le chiamate verso gli altri oggetti con cui collabora
Cosa sono? Sono finti oggetti che ci aiutano in vari modi a testare le nostre unità. Si chiamano simpaticamente: Dummy, Fake, Stub, Spy, Mock = Test Doubles
Utili perchè? Ci permettono di: rimanere focalizzati sul metodo che stiamo disegnando scrivere unit test anche su oggetti che collaborano con altri scrivere test di interazione tra diversi oggetti posticipare l’implementazione di alcuni collaboratori
Esempio (WheatherStatistics)
Ma hai presente quanto codice hai scritto? Ci sono alcuni framework che aiutano: RhinoMocks Moq TypeMock Nmock Nmock2 .NET Mock
Ri-Esempio (WheatherStatistics)
Le 3 fasi dell’apprendimento WTF !? **%*!! I got the power! Posso mockare il mondo! Lo zen e l’arte del Mockare quanto basta
Alcune (buone) regole Non verificare i dettagli della collaborazione Se puoi usa uno stub invece di un mock Non mockare grosse interfacce Mocka solo i tuoi diretti collaboratori Usa un solo mock per il test Non mockare i dati di ritorno
E i test di integrazione? I mock object permettono di scrivere test unitari, ma... ...anche i test di integrazione sono importanti.
Grazie Riferimenti: Growing object oriented software – Freeman, Pryce – Addison Wesley http://xunitpatterns.com/ http://www.mockobjects.com/

More Related Content

Similar to Mocking

Unit testing in Visual Studio 2013
Unit testing in Visual Studio 2013Unit testing in Visual Studio 2013
Unit testing in Visual Studio 2013DomusDotNet
 
Una fugace occhiata al Test Driven Development (2006)
Una fugace occhiata al Test Driven Development  (2006)Una fugace occhiata al Test Driven Development  (2006)
Una fugace occhiata al Test Driven Development (2006)Roberto Bettazzoni
 
Mamma, da grande voglio essere un Penetration Tester HackInBo 2016 Winter
Mamma, da grande voglio essere un Penetration Tester HackInBo  2016 WinterMamma, da grande voglio essere un Penetration Tester HackInBo  2016 Winter
Mamma, da grande voglio essere un Penetration Tester HackInBo 2016 WinterSimone Onofri
 
Test Driven Development @ Xe.Net
Test Driven Development @ Xe.NetTest Driven Development @ Xe.Net
Test Driven Development @ Xe.NetMauro Servienti
 
Fe05 test drivenjavascriptdevelopment
Fe05   test drivenjavascriptdevelopmentFe05   test drivenjavascriptdevelopment
Fe05 test drivenjavascriptdevelopmentDotNetCampus
 
Unit Test di Gabriele Seroni
Unit Test di Gabriele SeroniUnit Test di Gabriele Seroni
Unit Test di Gabriele SeroniGiuneco S.r.l
 
Delphi & Dintorni Webinar - Diventa un mago del Testing
Delphi & Dintorni Webinar - Diventa un mago del TestingDelphi & Dintorni Webinar - Diventa un mago del Testing
Delphi & Dintorni Webinar - Diventa un mago del TestingMarco Breveglieri
 
Software testing with mocking framework (Android App)
Software testing with mocking framework (Android App)Software testing with mocking framework (Android App)
Software testing with mocking framework (Android App)gioacchinolonardo
 
C#, imparare a programmare e sopravvivere
C#, imparare a programmare e sopravvivereC#, imparare a programmare e sopravvivere
C#, imparare a programmare e sopravvivereMatteo Valoriani
 
Lo sai che si può fare DDD in Javascript grazie a Typescript? Visual Studio e...
Lo sai che si può fare DDD in Javascript grazie a Typescript? Visual Studio e...Lo sai che si può fare DDD in Javascript grazie a Typescript? Visual Studio e...
Lo sai che si può fare DDD in Javascript grazie a Typescript? Visual Studio e...Marco Parenzan
 
AntiPatterns: i vizi del programmatore
AntiPatterns: i vizi del programmatoreAntiPatterns: i vizi del programmatore
AntiPatterns: i vizi del programmatoreManuel Scapolan
 
Agile Testing e UX: come ottenere il massimo da QA e Design
Agile Testing e UX: come ottenere il massimo da QA e DesignAgile Testing e UX: come ottenere il massimo da QA e Design
Agile Testing e UX: come ottenere il massimo da QA e DesignStefano Trojani
 
Lezione 10 - Programmazione Ad Oggetti, Vb Accenni
Lezione 10 - Programmazione Ad Oggetti, Vb AccenniLezione 10 - Programmazione Ad Oggetti, Vb Accenni
Lezione 10 - Programmazione Ad Oggetti, Vb AccenniRice Cipriani
 
TDD patterns and TDD strategies
TDD patterns and TDD strategiesTDD patterns and TDD strategies
TDD patterns and TDD strategiesAlessandro Ceseno
 

Similar to Mocking (20)

Unit testing in Visual Studio 2013
Unit testing in Visual Studio 2013Unit testing in Visual Studio 2013
Unit testing in Visual Studio 2013
 
Una fugace occhiata al Test Driven Development (2006)
Una fugace occhiata al Test Driven Development  (2006)Una fugace occhiata al Test Driven Development  (2006)
Una fugace occhiata al Test Driven Development (2006)
 
PhpUnit
PhpUnitPhpUnit
PhpUnit
 
TTT - Test, Tools and Tips - jug roma
TTT - Test, Tools and Tips - jug romaTTT - Test, Tools and Tips - jug roma
TTT - Test, Tools and Tips - jug roma
 
Software Testing e TDD
Software Testing e TDDSoftware Testing e TDD
Software Testing e TDD
 
Corso Java - Introduzione
Corso Java - IntroduzioneCorso Java - Introduzione
Corso Java - Introduzione
 
Mamma, da grande voglio essere un Penetration Tester HackInBo 2016 Winter
Mamma, da grande voglio essere un Penetration Tester HackInBo  2016 WinterMamma, da grande voglio essere un Penetration Tester HackInBo  2016 Winter
Mamma, da grande voglio essere un Penetration Tester HackInBo 2016 Winter
 
Test Driven Development @ Xe.Net
Test Driven Development @ Xe.NetTest Driven Development @ Xe.Net
Test Driven Development @ Xe.Net
 
Fe05 test drivenjavascriptdevelopment
Fe05   test drivenjavascriptdevelopmentFe05   test drivenjavascriptdevelopment
Fe05 test drivenjavascriptdevelopment
 
Unit Test di Gabriele Seroni
Unit Test di Gabriele SeroniUnit Test di Gabriele Seroni
Unit Test di Gabriele Seroni
 
Delphi & Dintorni Webinar - Diventa un mago del Testing
Delphi & Dintorni Webinar - Diventa un mago del TestingDelphi & Dintorni Webinar - Diventa un mago del Testing
Delphi & Dintorni Webinar - Diventa un mago del Testing
 
Testing
TestingTesting
Testing
 
Software testing with mocking framework (Android App)
Software testing with mocking framework (Android App)Software testing with mocking framework (Android App)
Software testing with mocking framework (Android App)
 
Le 12 pratiche
Le 12 praticheLe 12 pratiche
Le 12 pratiche
 
C#, imparare a programmare e sopravvivere
C#, imparare a programmare e sopravvivereC#, imparare a programmare e sopravvivere
C#, imparare a programmare e sopravvivere
 
Lo sai che si può fare DDD in Javascript grazie a Typescript? Visual Studio e...
Lo sai che si può fare DDD in Javascript grazie a Typescript? Visual Studio e...Lo sai che si può fare DDD in Javascript grazie a Typescript? Visual Studio e...
Lo sai che si può fare DDD in Javascript grazie a Typescript? Visual Studio e...
 
AntiPatterns: i vizi del programmatore
AntiPatterns: i vizi del programmatoreAntiPatterns: i vizi del programmatore
AntiPatterns: i vizi del programmatore
 
Agile Testing e UX: come ottenere il massimo da QA e Design
Agile Testing e UX: come ottenere il massimo da QA e DesignAgile Testing e UX: come ottenere il massimo da QA e Design
Agile Testing e UX: come ottenere il massimo da QA e Design
 
Lezione 10 - Programmazione Ad Oggetti, Vb Accenni
Lezione 10 - Programmazione Ad Oggetti, Vb AccenniLezione 10 - Programmazione Ad Oggetti, Vb Accenni
Lezione 10 - Programmazione Ad Oggetti, Vb Accenni
 
TDD patterns and TDD strategies
TDD patterns and TDD strategiesTDD patterns and TDD strategies
TDD patterns and TDD strategies
 

More from Emanuele DelBono

More from Emanuele DelBono (12)

The simplest thing that could possibly work
The simplest thing that could possibly workThe simplest thing that could possibly work
The simplest thing that could possibly work
 
Una crescita armoniosa
Una crescita armoniosaUna crescita armoniosa
Una crescita armoniosa
 
A sip of Elixir
A sip of ElixirA sip of Elixir
A sip of Elixir
 
React.js in real world apps.
React.js in real world apps. React.js in real world apps.
React.js in real world apps.
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
 
From ActiveRecord to EventSourcing
From ActiveRecord to EventSourcingFrom ActiveRecord to EventSourcing
From ActiveRecord to EventSourcing
 
Ruby seen by a C# developer
Ruby seen by a C# developerRuby seen by a C# developer
Ruby seen by a C# developer
 
Ruby loves DDD
Ruby loves DDDRuby loves DDD
Ruby loves DDD
 
An introduction to knockout.js
An introduction to knockout.jsAn introduction to knockout.js
An introduction to knockout.js
 
Node azure
Node azureNode azure
Node azure
 
Sinatra for REST services
Sinatra for REST servicesSinatra for REST services
Sinatra for REST services
 
Test driving an MVVM App
Test driving an MVVM AppTest driving an MVVM App
Test driving an MVVM App
 

Mocking

  • 1. mocking Emanuele DelBono emanuele@codiceplastico.com http://blog.codiceplastico.com
  • 3. Perchè? Se uno unit test è rossol’errore è sicuramentenell’unità sotto test
  • 4. Definizioni State Based Testing: verifica che un unità funzioni correttamente verificandone lo stato dopo l’esecuzione Interaction Testing: verifica che l’unità sotto test effettui correttamente le chiamate verso gli altri oggetti con cui collabora
  • 5. Cosa sono? Sono finti oggetti che ci aiutano in vari modi a testare le nostre unità. Si chiamano simpaticamente: Dummy, Fake, Stub, Spy, Mock = Test Doubles
  • 6. Utili perchè? Ci permettono di: rimanere focalizzati sul metodo che stiamo disegnando scrivere unit test anche su oggetti che collaborano con altri scrivere test di interazione tra diversi oggetti posticipare l’implementazione di alcuni collaboratori
  • 8. Ma hai presente quanto codice hai scritto? Ci sono alcuni framework che aiutano: RhinoMocks Moq TypeMock Nmock Nmock2 .NET Mock
  • 10. Le 3 fasi dell’apprendimento WTF !? **%*!! I got the power! Posso mockare il mondo! Lo zen e l’arte del Mockare quanto basta
  • 11. Alcune (buone) regole Non verificare i dettagli della collaborazione Se puoi usa uno stub invece di un mock Non mockare grosse interfacce Mocka solo i tuoi diretti collaboratori Usa un solo mock per il test Non mockare i dati di ritorno
  • 12. E i test di integrazione? I mock object permettono di scrivere test unitari, ma... ...anche i test di integrazione sono importanti.
  • 13. Grazie Riferimenti: Growing object oriented software – Freeman, Pryce – Addison Wesley http://xunitpatterns.com/ http://www.mockobjects.com/

Editor's Notes

  1. Dummy: servono da parametro per i metodi ma non sono usatiFake: sono implementati ma bypassano il «giro vero» (ad esempio in-memory db)Stub: rispondono in modo predefinitoSpy: rispondono in modo predefinito e registrano le chiamateMock: rispondono in modo predefinito e registrano le chiamate e fanno fallire i test se le chiamate non sono state fatte come previsto