0
STAATLICH
ANERKANNTE
FACHHOCHSCHULE
Author: Dip.-Inf. (FH) Johannes Hoppe
Date: 17.11.2010
01.12.2010
STUDIEREN
UND DURCHS...
STAATLICH
ANERKANNTE
FACHHOCHSCHULE
RIA – Rich Internet Applications
Author: Dip.-Inf. (FH) Johannes Hoppe
Date: 17.11.201...
Unit Tests in General
01
30.01.2015
Folie 3
Unit Tests in General
Dependencies
› Every meaningful piece of software has dependencies
to other pieces of software
30.01...
Unit Tests in General
Dependencies
› But usually software is tightly coupled to other software
› Tightly coupled software ...
Unit Tests in General
Dependencies
› We will concentrate on two types of dependencies:
1. A dependency by parameters:
Clas...
Unit Tests in General
Dependencies
2. A dependency by the internal use of the “new” keyword:
Class A {
Method() {
var c = ...
Unit Tests in General
Dependencies – Solution
› Our golden tools:
Interfaces & Dependency Injection
30.01.2015
Folie 8
So...
Unit Tests in General
Interfaces
› Interfaces are blueprints that describe a real implementation
› Method are going to be ...
Unit Tests in General
Dependency Injection
› We completely avoid the usage of the “new” keyword
› Instead, we are “injecti...
Unit Tests in General
Loosely Coupled
1. If all dependencies are hidden by Interfaces
2. We must not always use real-live ...
Unit Tests in General
30.01.2015
Folie 12
Definitions
› SUT == System Under Test
› Needs to be “fooled” so that it things ...
Unit Tests in General
30.01.2015
Folie 13
Definitions
› Stubs
› Simple hand-written test-objects
› Set up the environment ...
Unit Tests in General
30.01.2015
Folie 14
Definitions
› Mocks
› More complex test-objects
› pre-programmed with expectatio...
Unit Tests in General
Definitions
› Many people (including me) are mixing the terms stub, fake and mock
› Mocking framewor...
Unit Tests in General
Glossary
› MsTest – The unit testing framework that comes with Visual Studio
› Nunit – A quite popul...
Unit Tests in General
Questions
?
30.01.2015
Folie 17
Review of our first Unit Test
02
30.01.2015
Folie 18
Unit Tests
30.01.2015
Folie 19
How?
Review of our first Unit Test
Explanation of Code
30.01.2015
Folie 20
Review of our first Unit Test
Dependencies
30.01.2015
Folie 21
WebNoteRepository
IWebNote
IObjectSet<Note>
Review of our first Unit Test
Dependencies
IWebNote – Entity Framework:
IWebNote – by our own Mock:
30.01.2015
Folie 22
W...
Review of our first Unit Test
30.01.2015
Folie 23
WebNoteRepository
IWebNote
IObjectSet<Note>
Dependencies
IObjectSet<N...
Chaining
› Real live: by poor man’s DI
› Unit Test: automatically by the class WebNoteBaseRepositoryTest
› From which we a...
Chaining
Unit Tests in General
30.01.2015
Folie 25
Mocked Repository
Base class from which we are inheriting
Required to p...
Unit Tests
Your tasks from last week:
› Try to write your first set of unit tests
› Work together with you neighbor (2 per...
Unit Tests
03
30.01.2015
Folie 27
Unit Tests
Tests: only with stubs
30.01.2015
Folie 28
Unit Tests
30.01.2015
Folie 29
Unit Tests
30.01.2015
Folie 30
Unit Tests
30.01.2015
Folie 31
Unit Tests
30.01.2015
Folie 32
Unit Tests
30.01.2015
Folie 33
Unit Tests
Tests: with Mocks
30.01.2015
Folie 34
Unit Tests
30.01.2015
Folie 35
Unit Tests
30.01.2015
Folie 36
Unit Tests
30.01.2015
Folie 37
Unit Tests
30.01.2015
Folie 38
behavior verification! 
THANK YOU
FOR YOUR ATTENTION
30.01.2015
Folie 39
Upcoming SlideShare
Loading in...5
×

RIA 06 & 07 - Unit Testing in Detail

1,426

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

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

No notes for slide

Transcript of "RIA 06 & 07 - Unit Testing in Detail"

  1. 1. STAATLICH ANERKANNTE FACHHOCHSCHULE Author: Dip.-Inf. (FH) Johannes Hoppe Date: 17.11.2010 01.12.2010 STUDIEREN UND DURCHSTARTEN.
  2. 2. STAATLICH ANERKANNTE FACHHOCHSCHULE RIA – Rich Internet Applications Author: Dip.-Inf. (FH) Johannes Hoppe Date: 17.11.2010 01.12.2010
  3. 3. Unit Tests in General 01 30.01.2015 Folie 3
  4. 4. Unit Tests in General Dependencies › Every meaningful piece of software has dependencies to other pieces of software 30.01.2015 Folie 4 SoftwareA SoftwareB SoftwareC
  5. 5. Unit Tests in General Dependencies › But usually software is tightly coupled to other software › Tightly coupled software is untestable! 30.01.2015 Folie 5 SoftwareA SoftwareB SoftwareC
  6. 6. Unit Tests in General Dependencies › We will concentrate on two types of dependencies: 1. A dependency by parameters: ClassA { Method(ClassB b) {…}} 30.01.2015 Folie 6 SoftwareA SoftwareB SoftwareC
  7. 7. Unit Tests in General Dependencies 2. A dependency by the internal use of the “new” keyword: Class A { Method() { var c = new ClassC(); }} 30.01.2015 Folie 7 SoftwareA SoftwareB SoftwareC
  8. 8. Unit Tests in General Dependencies – Solution › Our golden tools: Interfaces & Dependency Injection 30.01.2015 Folie 8 SoftwareA ISoftwareB ISoftwareC SoftwareB SoftwareC
  9. 9. Unit Tests in General Interfaces › Interfaces are blueprints that describe a real implementation › Method are going to be independent from the concrete implementation of the parameter: ClassA { Method(ISoftwareB b) {…}} 30.01.2015 Folie 9 SoftwareA ISoftwareB ISoftwareC SoftwareB SoftwareC
  10. 10. Unit Tests in General Dependency Injection › We completely avoid the usage of the “new” keyword › Instead, we are “injecting” the required instance from outside by using Interfaces › “Poor Man’s”-DI through constructor (works for UT, but is an anti-pattern!) › by a framework (here: Unity Application Block): ClassA { [Dependency] public InterfaceC C { private get; set; } } 30.01.2015 Folie 10
  11. 11. Unit Tests in General Loosely Coupled 1. If all dependencies are hidden by Interfaces 2. We must not always use real-live objects 3. We could also “cheat” with any object we want (as long as it is implementing the interface)  Unit Testing!! 30.01.2015 Folie 11
  12. 12. Unit Tests in General 30.01.2015 Folie 12 Definitions › SUT == System Under Test › Needs to be “fooled” so that it things it is talking with real collaborators › Dummy › Objects passed around but never actually used › Used to fill required parameters in Unit Tests › Fake › Objects with working implementations, but not suitable for production (e.g. in memory database with hardcoded data) › Often used in software prototypes › Should not be used in unit tests, too
  13. 13. Unit Tests in General 30.01.2015 Folie 13 Definitions › Stubs › Simple hand-written test-objects › Set up the environment for the Unit Test by returning hardcoded values  can be used for: state verification  Determine whether the exercised method worked correctly by examining the state of the SUT and its collaborators after the method was exercised.
  14. 14. Unit Tests in General 30.01.2015 Folie 14 Definitions › Mocks › More complex test-objects › pre-programmed with expectations which form a specification of the calls they are expected to receive*  can be used for: behavior verification  Determine whether the exercised method worked correctly by checking if the SUT made the correct calls on the collaborators. (* according to the definition of Martin Fowler)
  15. 15. Unit Tests in General Definitions › Many people (including me) are mixing the terms stub, fake and mock › Mocking frameworks › Can create Mocks with a lot of magic inside › Can be used to build simple stubs, too! 30.01.2015 Folie 15
  16. 16. Unit Tests in General Glossary › MsTest – The unit testing framework that comes with Visual Studio › Nunit – A quite popular unit testing framework which is open source www.nunit.org › Moq –A mocking framework that easy to use because of the lambda expression syntax www.moq.me 30.01.2015 Folie 16
  17. 17. Unit Tests in General Questions ? 30.01.2015 Folie 17
  18. 18. Review of our first Unit Test 02 30.01.2015 Folie 18
  19. 19. Unit Tests 30.01.2015 Folie 19 How?
  20. 20. Review of our first Unit Test Explanation of Code 30.01.2015 Folie 20
  21. 21. Review of our first Unit Test Dependencies 30.01.2015 Folie 21 WebNoteRepository IWebNote IObjectSet<Note>
  22. 22. Review of our first Unit Test Dependencies IWebNote – Entity Framework: IWebNote – by our own Mock: 30.01.2015 Folie 22 WebNoteRepository IWebNote
  23. 23. Review of our first Unit Test 30.01.2015 Folie 23 WebNoteRepository IWebNote IObjectSet<Note> Dependencies IObjectSet<Note> – Entity Framework: IObjectSet<Note> – by our own Mock:
  24. 24. Chaining › Real live: by poor man’s DI › Unit Test: automatically by the class WebNoteBaseRepositoryTest › From which we are inheriting in our UT Review of our first Unit Test 30.01.2015 Folie 24
  25. 25. Chaining Unit Tests in General 30.01.2015 Folie 25 Mocked Repository Base class from which we are inheriting Required to prepare the mock
  26. 26. Unit Tests Your tasks from last week: › Try to write your first set of unit tests › Work together with you neighbor (2 persons, one team) › Use the Mock-Framework “Moq” from www.moq.me › Implement:  AddNoteTest, EditNoteTest, DeleteNoteTest  DeleteNoteShouldThrowExceptionTest  GetNoteShouldNotCallSaveChangesTest  6 Tests for the Controller 30.01.2015 Folie 26
  27. 27. Unit Tests 03 30.01.2015 Folie 27
  28. 28. Unit Tests Tests: only with stubs 30.01.2015 Folie 28
  29. 29. Unit Tests 30.01.2015 Folie 29
  30. 30. Unit Tests 30.01.2015 Folie 30
  31. 31. Unit Tests 30.01.2015 Folie 31
  32. 32. Unit Tests 30.01.2015 Folie 32
  33. 33. Unit Tests 30.01.2015 Folie 33
  34. 34. Unit Tests Tests: with Mocks 30.01.2015 Folie 34
  35. 35. Unit Tests 30.01.2015 Folie 35
  36. 36. Unit Tests 30.01.2015 Folie 36
  37. 37. Unit Tests 30.01.2015 Folie 37
  38. 38. Unit Tests 30.01.2015 Folie 38 behavior verification! 
  39. 39. THANK YOU FOR YOUR ATTENTION 30.01.2015 Folie 39
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×