when to mock · when to mock @stuherbert $ git clone git@github.com:foo/bar.git $ cd bar $ composer...

Post on 04-Jul-2020

22 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

@stuherbert

When To Mock

@stuherbert

Introductions

When To Mock

@stuherbert

of Unit TestingThe Holy Grail

When To Mock

@stuherbertWhen To Mock

$ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbertWhen To Mock

$ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbertWhen To Mock

$ git clone git@github.com:foo/bar.git $ cd bar $ composer install $ phpunit

@stuherbertWhen To Mock

$ 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

@stuherbert

This Was EasyIn The Pre-Web World

When To Mock

@stuherbert

• Our code wasn’t networked

• Our code used embedded data storage engines

• Our code wasn’t multi-user

When To Mock

@stuherbert

• Our code wasn’t networked

• Our code used embedded data storage engines

• Our code wasn’t multi-user

When To Mock

@stuherbert

• Our code wasn’t networked

• Our code used embedded data storage engines

• Our code wasn’t multi-user

When To Mock

@stuherbert

Self-ContainedOur Code Was

When To Mock

@stuherbert

Pre-Web Architecture Diagram

When To Mock

@stuherbert

Pre-Web Architecture Diagram

When To Mock

@stuherbert

Pre-Web Architecture Diagram

When To Mock

Application Stack

@stuherbert

Talks To ThingsToday’s Code

When To Mock

@stuherbert

Self ContainedToday’s Code Is Anything But

When To Mock

@stuherbert

Our Code Has Evolved From This

When To Mock

Application Stack

@stuherbert

Our Code Now Looks Like This

When To Mock

Database

Cache1

2

3

Internal Services

External Services

Controller Models

Views

Framework

LibrariesLibrariesLibraries

@stuherbert

Internally, More Complexity

When To Mock

Database

Cache1

2

3

Internal Services

External Services

Controller Models

Views

Framework

Libraries

@stuherbert

Internally, More Dependencies

When To Mock

Database

Cache1

2

3

Internal Services

External Services

Controller Models

Views

Framework

LibrariesLibrariesLibraries

@stuherbert

Storage Is Now A Service

When To Mock

Database

Cache1

2

3

Internal Services

External Services

Controller Models

Views

Framework

LibrariesLibrariesLibraries

@stuherbert

Functionality Is Now A Service

When To Mock

Database

Cache1

2

3

Internal Services

External Services

Controller Models

Views

Framework

LibrariesLibrariesLibraries

@stuherbert

Apps Are Now A Service To Users

When To Mock

Database

Cache1

2

3

Internal Services

External Services

Controller Models

Views

Framework

LibrariesLibrariesLibraries

@stuherbert

Unit TestsShould Execute Straight Out Of The Box

When To Mock

@stuherbert

Good Advice?Is This Still

When To Mock

@stuherbert

Still Good Advice?

• Makes testing reproducible

• Ease of use by new project members

• Supports continuous integration

When To Mock

@stuherbert

Still Good Advice?

• Makes testing reproducible

• Ease of use by new project members

• Supports continuous integration

When To Mock

@stuherbert

Still Good Advice?

• Makes testing reproducible

• Ease of use by new project members

• Supports continuous integration

When To Mock

@stuherbert

Unit TestsShould Execute Straight Out Of The Box

When To Mock

@stuherbert

Now Requires HelpMaking Unit Tests Execute Out Of The Box

When To Mock

@stuherbert

We Want To Unit Test Like This

When To Mock

Application Stack

@stuherbert

Are GoneBut Those Days

When To Mock

@stuherbert

Our App Now Needs These

When To Mock

Database

Cache

Internal Services

External Services

@stuherbert

If We Mock These

When To Mock

Database

Cache

Internal Services

External Services

@stuherbert

We Can Unit Test Without Them

When To Mock

Database

Cache

Internal Services

External Services

@stuherbert

& We Can Simulate Failure Too

When To Mock

Database

Cache

Internal Services

External Services

@stuherbert

Three Key Questions

When To Mock

@stuherbert

• Can we mock all the things?

• Should we mock all the things?

• Should we mock inside our app?

When To Mock

@stuherbert

• Can we mock all the things?

• Should we mock all the things?

• Should we mock inside our app?

When To Mock

@stuherbert

• Can we mock all the things?

• Should we mock all the things?

• Should we mock inside our app?

When To Mock

@stuherbert

All The Things?Can We Mock

When To Mock

@stuherbert

• Given enough time and effort, yes we can

When To Mock

@stuherbert

All The Things?Should We Mock

When To Mock

@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

@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

@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

@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

@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

@stuherbert

Test what we can,

mock what we have to

When To Mock

@stuherbert

Inside Our App?Should We Mock

When To Mock

@stuherbert

Our Code?What Happens Inside

When To Mock

@stuherbert

• computation

• branching

• input / output

When To Mock

@stuherbert

• computation

• branching

• input / output

When To Mock

@stuherbert

• computation

• branching

• input / output

When To Mock

@stuherbert

Forming Execution Paths

When To Mock

@stuherbert

One Execution PathEach Unit Test Exercises

When To Mock

@stuherbert

Such As This

When To Mock

@stuherbert

Or This

When To Mock

@stuherbert

Or This

When To Mock

@stuherbert

The Code You Call?What Happens When You Mock

When To Mock

@stuherbert

Adding MocksShortens The Paths We Can Test

When To Mock

@stuherbert

Take A Code Path

When To Mock

@stuherbert

Inject A Mock

When To Mock

@stuherbert

Shorter Code Path

When To Mock

@stuherbert

Behind The Mock?But What About The Code

When To Mock

@stuherbert

?

Unreachable Code

When To Mock

?

?

?

@stuherbert

Unreachable CodeMocks Inside Your App Create

When To Mock

@stuherbert

Is Untestable CodeUnreachable Code

When To Mock

@stuherbert

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

When To Mock

@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

@stuherbert

• How do you prove your mock behaves accurately today?

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

When To Mock

@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

@stuherbert

The Best ApproachMocks Are Sometimes

When To Mock

@stuherbert

Why?

When To Mock

@stuherbert

Is ReachableNot Every Code Path

When To Mock

@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

@stuherbert

To Test For FailureMocks Are A Great Way

When To Mock

@stuherbert

In Summary

When To Mock

@stuherbert

Test what we can,

mock what we have to

When To Mock

@stuherbert

Unit TestsShould Execute Straight Out Of The Box

When To Mock

@stuherbert

Unit TestsShould Execute Straight Out Of The Box

When To Mock

@stuherbert

Unit TestsShould Execute Straight Out Of The Box

As Long As Test Accuracy Is Not Compromised

When To Mock

@stuherbert

Unit TestsShould Be One Of Several Layers

In Your Test Strategy

When To Mock

@stuherbert

Use Other LayersTo Test For Things You Can’t Prove

Because Of Your Mocks

When To Mock

@stuherbert

• Storyplayer for factory acceptance testing

• Behat / BDD for product acceptance testing

When To Mock

@stuherbert

Thank YouAny Questions?

When To Mock

top related