Sergey Protko
Some stuff
- atomic behaviour
- verifies correctness of behaviour
- atomic behaviour
- verifies correctness of behaviour
When unit test fails
you should know exactly why it fails
Tests of interaction between components
Tests of interaction between components
Tests of interaction between components
J.B. Rainsberger
Growing object-oriented software guided by tests, Steve Freeman and Nat Pryce
Tests of system behaviour from user perspective
Provides External quality
Could be written by QA
// BAD
public function testPasses()
{
$obj = new MyClass();
$this->assertEquals(42, $obj->answer());
}
// GOOD
public function testGivesAnswerForLifeUniversAndEvrything()
{
$deepThought = new DeepThought();
$this->assertEquals(42, $deepThought->answer());
}
And they will...
If we are testing classes, we could have this levels of complexity if class has:
class RegiserUserAction
{
private $users;
private $emailValidator;
private $passwordEncoder;
private $notifier;
// constructor...
public function registerUser(string $email, string $password)
{
if (!$this->emailValidator->isEmailValid($email)
throw new InvalidEmailAddress($email);
$user = User::builder()
->withEmail($email)
->withPassword($this->passwordEncoder->encode($password)
->build();
$this->users->add($user);
$this->notifier->send(new WelcomeMessage($email));
}
}
class RegiserUserAction
{
private $users;
// constructor...
public function registerUser(EmailAddress $email, Password $password)
{
$user = User::builder()
->withEmail($email)
->withPassword($password)
->build();
$this->users->add($user);
}
}
public function getActiveDiscount(): Discount
{
$now = new \DateTime();
// ...
}
// pass data instead
public function getActiveDiscount(\DateTime $now): Discount
{
// ...
}
/**
* @dataProvider subtractionData
*/
public function testSupportsSubtraction(int $b, int $expected)
{
$number = new Number(10);
$actual = $number->sub($b);
$this->assertEquals($expected, $actual);
}
public function subtractionData()
{
return [
[1, 9],
[2, 8],
[7, 3],
];
}
public function command(): void
{
// do something with internal state
}
public function query(): int
{
return $this->someInternalState;
}
public function exceptThisCase(): int
{
return array_pop($this->someCollectionOfIntegers);
}
the wheel will spin for twenty seconds and the ball will stop on a random number
By Sergey Protko