Faking & Mocking

Techniques for Testing Code Safely and Effectively — C# Edition

Art for Faking & Mocking Check Out A Preview

By Mike Hill, Gil Broza

How do you write microtests against old, complicated code? Redesigning the code can help, yet often you'll still need to deal with awkward collaborators: calls to objects that are slow or difficult to test. Vanquish awkward collaborators by learning a family of fakes, including simulators, stubs, listening and talking fakes, mocks and auto-mocks.

Highlights

Expert Videos
Expert Videos: Learn tools & techniques by watching short videos from industry experts. Learn more...
Integrated Forums
Integrated Forums: Ask questions, get expert replies and read fascinating discussions. Learn more...

Includes

  • 4-7 hours of content
  • 145 pages of content
  • 11 Interactive quizzes
  • 4 Videos
  • 3 Exercises
  • Support for Visual Studio
  • Videos using ReSharper
  • Coverage of Moq

Contents

You Are Here 3
Roll The Dice: A Faking Opportunity 4
This Album 5
Roll The Dice Fakelessly 7
Download the Code 8
Steps For The Exercise 9
Our Solution 10
Points To Ponder 11
Awkward By Domain: The Maker & The Dice 13
One Weak Microtest 14
The Goal 15
Download the Code 16
Steps For The Exercise 17
Our Solution 18
Key Points For Your First Fake 19
Intro to Auto-Mocking 21
Some Details for the Automocker 22
Download the Code 23
Steps For The Exercise 24
Our Solution 25
Points To Ponder 26
Keyword: Awkward Collaboration ← Click to Preview 28
Awkward Results: Indeterminacy 29
Awkward Setup: Ugly Tests 30
Awkward Runtime: Slow = No 31
Awkward Control: Uncontrolled I/O 32
Trust Your Pain 33
Microtesting Awkward Collaborations 35
Real Life Vs. Test Life 36
The Big Idea 37
Types Of Fake 38
Fakes, Roses, And Terminology 39
Aren't You Really Hacking The Code? ← Click to Preview 40
Not Testing All 41
Microtesting Works 42
Slipping In A Fake 44
Keyword: Stub 45
An Awkward Collaboration 46
Awkward Enough For A Stub? 47
Keyword: Talking Fake 48
Example: InventoryControlTalkingFake 49
Taking It One Step Further 50
Keyword: Listening Fake 51
Example: OrderLogListeningFake 52
The Relationship Between Fake And Awkward ← Click to Preview 53
Refactoring To Facilitate Faking 55
Easy Slip #1: Method Argument Slip 56
Slipping Technique: Parameterize Method 57
Easy Slip #2: Constructor Argument Slip 58
Slipping Technique: Alternate Constructor 59
Slipping Technique: Passing A Factory 60
Problem: Several Awkward Collaborators Example 61
Solution: Several Awkward Collaborators Example 62
Slipping A Fake, Dependency Injection and Inversion Of Control ← Click to Preview 63
Slipping Technique: Introduce Adapter 64
Introduce Adapter Example 65
Adapter in Pictures 66
Review: Slipping Techniques 67
Introduction 69
The Code 70
Existing Microtests 71
Download The Code 72
The First Case Of Slowness 73
Our Solution 74
Great Microtests 75
Make A Stub For Shipper 76
Slipping A Shipper Stub Solution ← Click to Preview 77
Making a Listening Fake 78
Listening Fake Solution 79
Our Love/Hate Relationship With Singletons 81
Problems With Singletons 82
So, No Static Objects? 83
Solutions to Singletons 84
Slipping Technique: Alternate Construction 85
Slipping Technique: Set Fake Singleton 86
Set Fake Singleton Example ← Click to Preview 87
Slipping Technique: Redirection 88
Redirection Example 89
Static Collaborator: Another Example 90
Static Collaborator: Possible Solution 91
Exercise: Faking A Singleton 93
A First Attempt 94
Download The Code 95
Double Awkwardness 96
Hints 97
Our Solution 98
A Little Story 100
What It Looks Like 101
Robin To The Rescue: FakeTransactionLog 102
The Same Test Every Time 103
What To Do? 104
Keyword: Mock 105
Is That All? 106
Big Idea: Interaction, Not State ← Click to Preview 107
Big Idea: Mocks Can Be Made Automagically 108
How Are Mocks Born? 109
What are Auto-Mocks? 111
Five Steps To Mocking 112
Introducing Moq by Example 113
Creating a Stub 114
Creating a Mock 115
Creating a Partial Mock 116
Method Return Values: Can You Hear Me Now? 117
Yowza! 118
Let's Play 'Find The Mock' 119
Quick Reference: Matchers 120
Throwing Exceptions 121
Verifying After Use 122
Automocking Expectations 124
The Challenge 125
The Code 126
Awkwardness ← Click to Preview 127
Download The Code 128
Your Mission: The Simplest Case 129
Solution For The Simplest Case 130
The Second Task 131
Solution For The Second Task 132
The Last Task 133
Solution For The Last Task 134
A Hardware Situation 136
Keyword: Simulator 137
Simulators In Practice 138
The Real Role Of Simulators 139
Real Objects vs. Fakes & Mocks 141
An Example 142
A Defect Slips Through 143
Conclusion: What's Next? 145

Related Items

Volume Licensing

Have Any Questions?

Highlights

Expert Videos
Expert Videos: Learn tools & techniques by watching short videos from industry experts. Learn more...
Integrated Forums
Integrated Forums: Ask questions, get expert replies and read fascinating discussions. Learn more...

Includes

  • 4-7 hours of content
  • 145 pages of content
  • 11 Interactive quizzes
  • 4 Videos
  • 3 Exercises
  • Support for Visual Studio
  • Videos using ReSharper
  • Coverage of Moq

Contents

You Are Here 3
Roll The Dice: A Faking Opportunity 4
This Album 5
Roll The Dice Fakelessly 7
Download the Code 8
Steps For The Exercise 9
Our Solution 10
Points To Ponder 11
Awkward By Domain: The Maker & The Dice 13
One Weak Microtest 14
The Goal 15
Download the Code 16
Steps For The Exercise 17
Our Solution 18
Key Points For Your First Fake 19
Intro to Auto-Mocking 21
Some Details for the Automocker 22
Download the Code 23
Steps For The Exercise 24
Our Solution 25
Points To Ponder 26
Keyword: Awkward Collaboration ← Click to Preview 28
Awkward Results: Indeterminacy 29
Awkward Setup: Ugly Tests 30
Awkward Runtime: Slow = No 31
Awkward Control: Uncontrolled I/O 32
Trust Your Pain 33
Microtesting Awkward Collaborations 35
Real Life Vs. Test Life 36
The Big Idea 37
Types Of Fake 38
Fakes, Roses, And Terminology 39
Aren't You Really Hacking The Code? ← Click to Preview 40
Not Testing All 41
Microtesting Works 42
Slipping In A Fake 44
Keyword: Stub 45
An Awkward Collaboration 46
Awkward Enough For A Stub? 47
Keyword: Talking Fake 48
Example: InventoryControlTalkingFake 49
Taking It One Step Further 50
Keyword: Listening Fake 51
Example: OrderLogListeningFake 52
The Relationship Between Fake And Awkward ← Click to Preview 53
Refactoring To Facilitate Faking 55
Easy Slip #1: Method Argument Slip 56
Slipping Technique: Parameterize Method 57
Easy Slip #2: Constructor Argument Slip 58
Slipping Technique: Alternate Constructor 59
Slipping Technique: Passing A Factory 60
Problem: Several Awkward Collaborators Example 61
Solution: Several Awkward Collaborators Example 62
Slipping A Fake, Dependency Injection and Inversion Of Control ← Click to Preview 63
Slipping Technique: Introduce Adapter 64
Introduce Adapter Example 65
Adapter in Pictures 66
Review: Slipping Techniques 67
Introduction 69
The Code 70
Existing Microtests 71
Download The Code 72
The First Case Of Slowness 73
Our Solution 74
Great Microtests 75
Make A Stub For Shipper 76
Slipping A Shipper Stub Solution ← Click to Preview 77
Making a Listening Fake 78
Listening Fake Solution 79
Our Love/Hate Relationship With Singletons 81
Problems With Singletons 82
So, No Static Objects? 83
Solutions to Singletons 84
Slipping Technique: Alternate Construction 85
Slipping Technique: Set Fake Singleton 86
Set Fake Singleton Example ← Click to Preview 87
Slipping Technique: Redirection 88
Redirection Example 89
Static Collaborator: Another Example 90
Static Collaborator: Possible Solution 91
Exercise: Faking A Singleton 93
A First Attempt 94
Download The Code 95
Double Awkwardness 96
Hints 97
Our Solution 98
A Little Story 100
What It Looks Like 101
Robin To The Rescue: FakeTransactionLog 102
The Same Test Every Time 103
What To Do? 104
Keyword: Mock 105
Is That All? 106
Big Idea: Interaction, Not State ← Click to Preview 107
Big Idea: Mocks Can Be Made Automagically 108
How Are Mocks Born? 109
What are Auto-Mocks? 111
Five Steps To Mocking 112
Introducing Moq by Example 113
Creating a Stub 114
Creating a Mock 115
Creating a Partial Mock 116
Method Return Values: Can You Hear Me Now? 117
Yowza! 118
Let's Play 'Find The Mock' 119
Quick Reference: Matchers 120
Throwing Exceptions 121
Verifying After Use 122
Automocking Expectations 124
The Challenge 125
The Code 126
Awkwardness ← Click to Preview 127
Download The Code 128
Your Mission: The Simplest Case 129
Solution For The Simplest Case 130
The Second Task 131
Solution For The Second Task 132
The Last Task 133
Solution For The Last Task 134
A Hardware Situation 136
Keyword: Simulator 137
Simulators In Practice 138
The Real Role Of Simulators 139
Real Objects vs. Fakes & Mocks 141
An Example 142
A Defect Slips Through 143
Conclusion: What's Next? 145

Related Items