Slide 1

Slide 1 text

TDD 101 4. Testing the unpredictable

Slide 2

Slide 2 text

Testing the unpredictable

Slide 3

Slide 3 text

Testing the unpredictable Elements beyond our system: • Randomness • Time • File system You can’t rely on their behavior

Slide 4

Slide 4 text

How can we test?

Slide 5

Slide 5 text

1. Things that are using unpredictable components They are global state dependencies Isolate and extract Invert dependency Stub for testing

Slide 6

Slide 6 text

Example class User { private $id; private $name; public function __construct(string $name) { $this->id = Uuid::uuid4(); $this->name = $name; } }

Slide 7

Slide 7 text

Example class User { private $id; private $name; public function __construct( UserId $id, string $name ) { $this->id = $id; $this->name = $name; } }

Slide 8

Slide 8 text

2. Things that are unpredictable Don’t test what you don’t own Test for properties of the output, not the output itself

Slide 9

Slide 9 text

Example class PasswordGeneratorTest extends TestCase { public function testShouldBeLongEnough() { $password = $this->generator->generate(); $this->assertTrue(strlen($password) > 9); } }

Slide 10

Slide 10 text

Avoid coupling with dependency inversion

Slide 11

Slide 11 text

Dependency Inversion High level modules should not depend on low level modules, both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.

Slide 12

Slide 12 text

Thigh coupling MyClass Dependency Uses

Slide 13

Slide 13 text

Dependency inversion MyClass Interface Dependency Uses Implements

Slide 14

Slide 14 text

Dependency inversion & adapter pattern MyClass Interface Dependency Uses Implements Adapter Uses

Slide 15

Slide 15 text

No content