Test Unitari
Per unit testing (testing d'unità o testing unitario) si intende l'attività di test di singole unità software.
Per unità si intende normalmente il minimo componente di un programma dotato di funzionamento autonomo (una Classe!)
rif. https://it.wikipedia.org/wiki/Unit_testing
Cosa vuol dire?
Fare un test unitario vuol dire "ignorare" il resto della nostra applicazione.
Il test unitario controlla che il nostro codice faccia esattamente quello che ci aspettiamo a livello LOGICO.
Unit tests tell a developer that the code is doing things right; (*)
Stubs
Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.
rif. https://martinfowler.com/articles/mocksArentStubs.html
A cosa serve fare il mocking di oggetti?
- Rimpiazzare un comportamento non deterministico (l'ora o la temperatura ambiente).
- Se l'oggetto ha degli stati difficili da riprodurre (simulare un eccezione).
2 chiacchiere su un caso semi reale
class ConsentsBagStrategy
{
/** @var ConsentsStrategy */
private $consentsStrategy;
public function __construct(
ConsentsStrategy $ConsentsStrategy
) {
$this->consentsStrategy = $ConsentsStrategy;
}
public function applyStrategy(
?ConsentsBag $previousConsentsBag,
?ConsentsBag $newConsentsBag): ?ConsentsBag
{
if (null === $newConsentsBag) {
return $previousConsentsBag;
}
if (null === $previousConsentsBag) {
return $newConsentsBag;
}
$this->mergeConsents($previousConsentsBag, $newConsentsBag->getConsents());
return $previousConsentsBag;
}
private function mergeConsents(ConsentsBag $consentsBag, array $Consents): void
{
$consents = $this->consentsStrategy->merge($consentsBag->getConsents(), $Consents);
foreach ($consents as $businessUnit => $consent) {
$consentsBag->setConsent($consent, $businessUnit);
}
}
}