when to mock · when to mock @stuherbert $ git clone [email protected]:foo/bar.git $ cd bar $ composer...

88
@stuherbert When To Mock

Upload: others

Post on 04-Jul-2020

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

When To Mock

Page 2: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Introductions

When To Mock

Page 3: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

of Unit TestingThe Holy Grail

When To Mock

Page 4: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbertWhen To Mock

$ git clone [email protected]:foo/bar.git $ cd bar $ composer install $ phpunit

Page 5: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbertWhen To Mock

$ git clone [email protected]:foo/bar.git $ cd bar $ composer install $ phpunit

Page 6: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbertWhen To Mock

$ git clone [email protected]:foo/bar.git $ cd bar $ composer install $ phpunit

Page 7: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbertWhen To Mock

$ git clone [email protected]:foo/bar.git $ cd bar $ composer install $ phpunit

Page 8: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Unit TestsShould Execute Straight Out Of The Box

When To Mock

Page 9: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

This Was EasyIn The Pre-Web World

When To Mock

Page 10: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• Our code wasn’t networked

• Our code used embedded data storage engines

• Our code wasn’t multi-user

When To Mock

Page 11: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• Our code wasn’t networked

• Our code used embedded data storage engines

• Our code wasn’t multi-user

When To Mock

Page 12: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• Our code wasn’t networked

• Our code used embedded data storage engines

• Our code wasn’t multi-user

When To Mock

Page 13: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Self-ContainedOur Code Was

When To Mock

Page 14: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Pre-Web Architecture Diagram

When To Mock

Page 15: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Pre-Web Architecture Diagram

When To Mock

Page 16: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Pre-Web Architecture Diagram

When To Mock

Application Stack

Page 17: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Talks To ThingsToday’s Code

When To Mock

Page 18: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Self ContainedToday’s Code Is Anything But

When To Mock

Page 19: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Our Code Has Evolved From This

When To Mock

Application Stack

Page 20: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Our Code Now Looks Like This

When To Mock

Database

Cache1

2

3

Internal Services

External Services

Controller Models

Views

Framework

LibrariesLibrariesLibraries

Page 21: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Internally, More Complexity

When To Mock

Database

Cache1

2

3

Internal Services

External Services

Controller Models

Views

Framework

Libraries

Page 22: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Internally, More Dependencies

When To Mock

Database

Cache1

2

3

Internal Services

External Services

Controller Models

Views

Framework

LibrariesLibrariesLibraries

Page 23: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Storage Is Now A Service

When To Mock

Database

Cache1

2

3

Internal Services

External Services

Controller Models

Views

Framework

LibrariesLibrariesLibraries

Page 24: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Functionality Is Now A Service

When To Mock

Database

Cache1

2

3

Internal Services

External Services

Controller Models

Views

Framework

LibrariesLibrariesLibraries

Page 25: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Apps Are Now A Service To Users

When To Mock

Database

Cache1

2

3

Internal Services

External Services

Controller Models

Views

Framework

LibrariesLibrariesLibraries

Page 26: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Unit TestsShould Execute Straight Out Of The Box

When To Mock

Page 27: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Good Advice?Is This Still

When To Mock

Page 28: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Still Good Advice?

• Makes testing reproducible

• Ease of use by new project members

• Supports continuous integration

When To Mock

Page 29: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Still Good Advice?

• Makes testing reproducible

• Ease of use by new project members

• Supports continuous integration

When To Mock

Page 30: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Still Good Advice?

• Makes testing reproducible

• Ease of use by new project members

• Supports continuous integration

When To Mock

Page 31: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Unit TestsShould Execute Straight Out Of The Box

When To Mock

Page 32: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Now Requires HelpMaking Unit Tests Execute Out Of The Box

When To Mock

Page 33: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

We Want To Unit Test Like This

When To Mock

Application Stack

Page 34: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Are GoneBut Those Days

When To Mock

Page 35: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Our App Now Needs These

When To Mock

Database

Cache

Internal Services

External Services

Page 36: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

If We Mock These

When To Mock

Database

Cache

Internal Services

External Services

Page 37: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

We Can Unit Test Without Them

When To Mock

Database

Cache

Internal Services

External Services

Page 38: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

& We Can Simulate Failure Too

When To Mock

Database

Cache

Internal Services

External Services

Page 39: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Three Key Questions

When To Mock

Page 40: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• Can we mock all the things?

• Should we mock all the things?

• Should we mock inside our app?

When To Mock

Page 41: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• Can we mock all the things?

• Should we mock all the things?

• Should we mock inside our app?

When To Mock

Page 42: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• Can we mock all the things?

• Should we mock all the things?

• Should we mock inside our app?

When To Mock

Page 43: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

All The Things?Can We Mock

When To Mock

Page 44: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• Given enough time and effort, yes we can

When To Mock

Page 45: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

All The Things?Should We Mock

When To Mock

Page 46: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• Who is going to build the mocks?

• How do you prove your mock behaves accurately today?

• How do you prove your mock still behaves accurately tomorrow?

When To Mock

Page 47: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• Who is going to build the mocks?

• How do you prove your mock behaves accurately today?

• How do you prove your mock still behaves accurately tomorrow?

When To Mock

Page 48: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• Who is going to build the mocks?

• How do you prove your mock behaves accurately today?

• How do you prove your mock still behaves accurately tomorrow?

When To Mock

Page 49: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• A mock can only be as good as the author’s understanding of whatever is being mocked

• Tests using rotted mocks will continue to pass, but the code will fail when shipped

When To Mock

Page 50: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• A mock can only be as good as the author’s understanding of whatever is being mocked

• Tests using rotted mocks will continue to pass, but the code will fail when shipped

When To Mock

Page 51: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Test what we can,

mock what we have to

When To Mock

Page 52: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Inside Our App?Should We Mock

When To Mock

Page 53: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Our Code?What Happens Inside

When To Mock

Page 54: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• computation

• branching

• input / output

When To Mock

Page 55: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• computation

• branching

• input / output

When To Mock

Page 56: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• computation

• branching

• input / output

When To Mock

Page 57: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Forming Execution Paths

When To Mock

Page 58: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

One Execution PathEach Unit Test Exercises

When To Mock

Page 59: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Such As This

When To Mock

Page 60: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Or This

When To Mock

Page 61: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Or This

When To Mock

Page 62: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

The Code You Call?What Happens When You Mock

When To Mock

Page 63: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Adding MocksShortens The Paths We Can Test

When To Mock

Page 64: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Take A Code Path

When To Mock

Page 65: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Inject A Mock

When To Mock

Page 66: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Shorter Code Path

When To Mock

Page 67: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Behind The Mock?But What About The Code

When To Mock

Page 68: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

?

Unreachable Code

When To Mock

?

?

?

Page 69: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Unreachable CodeMocks Inside Your App Create

When To Mock

Page 70: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Is Untestable CodeUnreachable Code

When To Mock

Page 71: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

What Is The RiskFrom The Code That You Can’t Test?

When To Mock

Page 72: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Key Questions

• How do you prove your mock behaves accurately today?

• How do you prove your mock still behaves accurately tomorrow?

When To Mock

Page 73: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• How do you prove your mock behaves accurately today?

• How do you prove your mock still behaves accurately tomorrow?

When To Mock

Page 74: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• A mock can only be as good as the author’s understanding of whatever is being mocked

• Tests using rotted mocks will continue to pass, but the code will fail when shipped

When To Mock

Page 75: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

The Best ApproachMocks Are Sometimes

When To Mock

Page 76: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Why?

When To Mock

Page 77: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Is ReachableNot Every Code Path

When To Mock

Page 78: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Testing For Failure

• How do you get bad responses from the code you call?

• How do you trigger your error handling?

When To Mock

Page 79: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

To Test For FailureMocks Are A Great Way

When To Mock

Page 80: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

In Summary

When To Mock

Page 81: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Test what we can,

mock what we have to

When To Mock

Page 82: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Unit TestsShould Execute Straight Out Of The Box

When To Mock

Page 83: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Unit TestsShould Execute Straight Out Of The Box

When To Mock

Page 84: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Unit TestsShould Execute Straight Out Of The Box

As Long As Test Accuracy Is Not Compromised

When To Mock

Page 85: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Unit TestsShould Be One Of Several Layers

In Your Test Strategy

When To Mock

Page 86: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Use Other LayersTo Test For Things You Can’t Prove

Because Of Your Mocks

When To Mock

Page 87: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

• Storyplayer for factory acceptance testing

• Behat / BDD for product acceptance testing

When To Mock

Page 88: When To Mock · When To Mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbert

Thank YouAny Questions?

When To Mock