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.