mock objects presentation

10
Mock Objects and Mock Frameworks Andriy Buday

Upload: andriy-buday

Post on 10-May-2015

1.021 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Mock Objects Presentation

Mock Objects and Mock Frameworks

Andriy Buday

Page 2: Mock Objects Presentation

Weather Service Example

It is still raining in Lviv So how to test sunny weather?

Introduction…

Page 3: Mock Objects Presentation

What are Mock Objects?

Any dummy object that stands in for a real object that is not available, or is difficult to use in a test case

A mock object must have ability to set up expectations and provide a self-validation mechanism

Page 4: Mock Objects Presentation

Why use mock objects?

Help keep design decoupled Check code’s usage of another object Make tests run faster Make it easier to write code for

problematic resources Defer having to write a class Test components in isolation from the rest

of the system

Page 5: Mock Objects Presentation

Why use mock objects?

Promote interface-based design Refine interfaces Test unusual, unlikely and exceptional

situations

Page 6: Mock Objects Presentation

When to use Mock Objects?

Non-deterministic or random behaviour Difficult setup Behavior hard to cause Slow real objects UIs Test needs to make queries not supported

by real object Non-existent real objects

Page 7: Mock Objects Presentation

Diff Between Mocks and Stubs

Dummyare not really used

Fakehave working implementation but not real

Stubspass prepared answers

Mocksprovide expectation logic

Page 8: Mock Objects Presentation

Warehouse Example

Regular tests Nmock Rhino Mocks Paskal Mock

Page 9: Mock Objects Presentation

Rhino Mocks

Dynamic mocks1. on concrete class

2. mock some methods

Partial mocks1. on class/interface

2. every member should be mocks

3. compiler default for not expected methods called

Strict mocks1. similar to partial

2. test fail if not expected method was called

Rhino can kill

weather

dilemma

easily

Page 10: Mock Objects Presentation

Questions?