Top Banner
STAATLICH ANERKANNTE FACHHOCHSCHULE Author: Dip.-Inf. (FH) Johannes Hoppe Date: 17.11.2010 01.12.2010 STUDIEREN UND DURCHSTARTEN.
39
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RIA 06 & 07 - Unit Testing in Detail

STAATLICHANERKANNTEFACHHOCHSCHULE

Author: Dip.-Inf. (FH) Johannes HoppeDate: 17.11.2010

01.12.2010

STUDIERENUND DURCHSTARTEN.

Page 2: RIA 06 & 07 - Unit Testing in Detail

STAATLICHANERKANNTEFACHHOCHSCHULE

RIA – Rich Internet Applications

Author: Dip.-Inf. (FH) Johannes HoppeDate: 17.11.2010

01.12.2010

Page 3: RIA 06 & 07 - Unit Testing in Detail

Unit Tests in General

0109.04.2023

Folie 3

Page 4: RIA 06 & 07 - Unit Testing in Detail

Unit Tests in General

Dependencies

› Every meaningful piece of software has dependenciesto other pieces of software

09.04.2023

Folie 4

SoftwareA

SoftwareB

SoftwareC

Page 5: RIA 06 & 07 - Unit Testing in Detail

Unit Tests in General

Dependencies

› But usually software is tightly coupled to other software› Tightly coupled software is untestable!

09.04.2023

Folie 5

SoftwareA

SoftwareB

SoftwareC

Johannes
Gründe in Diskussion erarbeiten!
Page 6: RIA 06 & 07 - Unit Testing in Detail

Unit Tests in General

Dependencies

› We will concentrate on two types of dependencies:

1. A dependency by parameters:ClassA { Method(ClassB b) {…}}

09.04.2023

Folie 6

SoftwareA

SoftwareB

SoftwareC

Page 7: RIA 06 & 07 - Unit Testing in Detail

Unit Tests in General

Dependencies

2. A dependency by the internal use of the “new” keyword:Class A { Method() { var c = new ClassC();}}

09.04.2023

Folie 7

SoftwareA

SoftwareB

SoftwareC

Page 8: RIA 06 & 07 - Unit Testing in Detail

Unit Tests in General

Dependencies – Solution

› Our golden tools:

Interfaces & Dependency Injection

09.04.2023

Folie 8

SoftwareA

ISoftwareB

ISoftwareC

SoftwareB

SoftwareC

Page 9: RIA 06 & 07 - Unit Testing in Detail

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) {…}}

09.04.2023

Folie 9

SoftwareA

ISoftwareB

ISoftwareC

SoftwareB

SoftwareC

Page 10: RIA 06 & 07 - Unit Testing in Detail

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

09.04.2023

Folie 10

Page 11: RIA 06 & 07 - Unit Testing in Detail

Unit Tests in General

Loosely Coupled

1. If all dependencies are hidden by Interfaces2. We must not always use real-live objects3. We could also “cheat” with any object we want

(as long as it is implementing the interface)

Unit Testing!!

09.04.2023

Folie 11

Page 12: RIA 06 & 07 - Unit Testing in Detail

Unit Tests in General

09.04.2023

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

Page 13: RIA 06 & 07 - Unit Testing in Detail

Unit Tests in General

09.04.2023

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 byexamining the state of the SUT and its collaboratorsafter the method was exercised.

Page 14: RIA 06 & 07 - Unit Testing in Detail

Unit Tests in General

09.04.2023

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 bychecking if the SUT made thecorrect calls on the collaborators.

(* according to the definition of Martin Fowler)

Page 15: RIA 06 & 07 - Unit Testing in Detail

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!

09.04.2023

Folie 15

Page 16: RIA 06 & 07 - Unit Testing in Detail

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 sourcewww.nunit.org

› Moq – A mocking framework that easy to use because of the lambda expression syntax

www.moq.me

09.04.2023

Folie 16

Page 17: RIA 06 & 07 - Unit Testing in Detail

Unit Tests in General

Questions

?

09.04.2023

Folie 17

Page 18: RIA 06 & 07 - Unit Testing in Detail

Review of our first Unit Test

0209.04.2023

Folie 18

Page 19: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

09.04.2023

Folie 19

Ho

w?

Page 20: RIA 06 & 07 - Unit Testing in Detail

Review of our first Unit Test

Explanation of Code

09.04.2023

Folie 20

Page 21: RIA 06 & 07 - Unit Testing in Detail

Review of our first Unit Test

Dependencies

09.04.2023

Folie 21

WebNoteRepository

IWebNote

IObjectSet<Note>

Page 22: RIA 06 & 07 - Unit Testing in Detail

Review of our first Unit Test

Dependencies

IWebNote – Entity Framework:

IWebNote – by our own Mock:

09.04.2023

Folie 22

WebNoteRepository

IWebNote

Page 23: RIA 06 & 07 - Unit Testing in Detail

Review of our first Unit Test

09.04.2023

Folie 23

WebNoteRepository

IWebNote

IObjectSet<Note>

Dependencies

IObjectSet<Note> – Entity Framework:

IObjectSet<Note> – by our own Mock:

Page 24: RIA 06 & 07 - Unit Testing in Detail

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

09.04.2023

Folie 24

Page 25: RIA 06 & 07 - Unit Testing in Detail

Chaining

Unit Tests in General

09.04.2023

Folie 25

Mocked Repository

Base class from which we are inheriting

Required to prepare the mock

Page 26: RIA 06 & 07 - Unit Testing in Detail

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

09.04.2023

Folie 26

Page 27: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

0309.04.2023

Folie 27

Page 28: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

Tests: only with stubs

09.04.2023

Folie 28

Page 29: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

09.04.2023

Folie 29

Page 30: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

09.04.2023

Folie 30

Page 31: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

09.04.2023

Folie 31

Page 32: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

09.04.2023

Folie 32

Page 33: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

09.04.2023

Folie 33

Page 34: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

Tests: with Mocks

09.04.2023

Folie 34

Page 35: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

09.04.2023

Folie 35

Page 36: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

09.04.2023

Folie 36

Page 37: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

09.04.2023

Folie 37

Page 38: RIA 06 & 07 - Unit Testing in Detail

Unit Tests

09.04.2023

Folie 38

behavior verification!

Page 39: RIA 06 & 07 - Unit Testing in Detail

THANK YOUFOR YOUR ATTENTION

09.04.2023

Folie 39