Testing doubles: Difference between revisions
Added category IT backend architecture |
No edit summary |
||
| Line 1: | Line 1: | ||
Test doubles are substitutes for components in software testing, used to isolate a unit of code from its dependencies to achieve controlled and deterministic test conditions. They emulate the behavior of real components, ensuring the test focuses only on the unit under examination. The term | Test doubles are substitutes for components in software testing, used to isolate a unit of code from its dependencies to achieve controlled and deterministic test conditions. They emulate the behavior of real components, ensuring the test focuses only on the unit under examination. The term "test double" encompasses various specific types: | ||
<span id="dummy"></span> | <span id="dummy"></span> | ||
| Line 6: | Line 6: | ||
Implementation or replica of another object for having an entity of the same type as the original. It doesn't have any logic and their methods return null. They are usually used to fill functions' parameters list and are never actually used. | Implementation or replica of another object for having an entity of the same type as the original. It doesn't have any logic and their methods return null. They are usually used to fill functions' parameters list and are never actually used. | ||
<syntaxhighlight>ValidateAuthDummy { authorize($user) { return null; } }</syntaxhighlight> | <syntaxhighlight lang="php">ValidateAuthDummy { authorize($user) { return null; } }</syntaxhighlight> | ||
<span id="stub"></span> | <span id="stub"></span> | ||
== Stub == | == Stub == | ||
| Line 12: | Line 12: | ||
Similar to dummy, but in this case methods return a value that is interesting for us. They are useful for those occasions that we already have something validated but we need to make a bypass of that validation. | Similar to dummy, but in this case methods return a value that is interesting for us. They are useful for those occasions that we already have something validated but we need to make a bypass of that validation. | ||
<syntaxhighlight>ValidateAuthStub { authorize($user) { return true; } }</syntaxhighlight> | <syntaxhighlight lang="php">ValidateAuthStub { authorize($user) { return true; } }</syntaxhighlight> | ||
<span id="spies"></span> | <span id="spies"></span> | ||
== Spies == | == Spies == | ||
| Line 23: | Line 23: | ||
It is a clone of an existing object. In it you predefine some values. It will be use to check the logic of a service, due to we already know what should be returned. | It is a clone of an existing object. In it you predefine some values. It will be use to check the logic of a service, due to we already know what should be returned. | ||
<syntaxhighlight>is_user_authorized() { | <syntaxhighlight lang="php">is_user_authorized() { | ||
$mockUser = new User().grantAuthorization(); | $mockUser = new User().grantAuthorization(); | ||
assert(true, isUserAuthorized($mockUser)); | assert(true, isUserAuthorized($mockUser)); | ||
| Line 34: | Line 34: | ||
Simulate the complete behaviour of an object with invented data. It can contain predefined logic to simulate the behaviour of the replicated object. | Simulate the complete behaviour of an object with invented data. It can contain predefined logic to simulate the behaviour of the replicated object. | ||
<syntaxhighlight>ValidateAuthStub { | <syntaxhighlight lang="php">ValidateAuthStub { | ||
authorize($user) { | authorize($user) { | ||
return $user == ‘Bob’; | return $user == ‘Bob’; | ||
} | } | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
[[Category:IT backend architecture]] | [[Category:IT backend architecture]] | ||
Latest revision as of 17:30, 22 October 2025
Test doubles are substitutes for components in software testing, used to isolate a unit of code from its dependencies to achieve controlled and deterministic test conditions. They emulate the behavior of real components, ensuring the test focuses only on the unit under examination. The term "test double" encompasses various specific types:
Dummy
Implementation or replica of another object for having an entity of the same type as the original. It doesn't have any logic and their methods return null. They are usually used to fill functions' parameters list and are never actually used.
ValidateAuthDummy { authorize($user) { return null; } }
Stub
Similar to dummy, but in this case methods return a value that is interesting for us. They are useful for those occasions that we already have something validated but we need to make a bypass of that validation.
ValidateAuthStub { authorize($user) { return true; } }
Spies
Are stubs that also record and log some information based on how they were called.
Mock
It is a clone of an existing object. In it you predefine some values. It will be use to check the logic of a service, due to we already know what should be returned.
is_user_authorized() {
$mockUser = new User().grantAuthorization();
assert(true, isUserAuthorized($mockUser));
}They are specially abused because it's very easy to misuse them by writing tests to simply assert the specific details of the workings of some code rather that its interactions with collaborators. If implementation changes, the test breaks.
Fake
Simulate the complete behaviour of an object with invented data. It can contain predefined logic to simulate the behaviour of the replicated object.
ValidateAuthStub {
authorize($user) {
return $user == ‘Bob’;
}
}