Testing doubles: Difference between revisions

From A Technological Wiki
Jump to navigation Jump to search
imported>Unknown user
No edit summary
imported>Unknown user
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 double" encompasses various specific types:


<span id="dummy"></span>
== 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.
<syntaxhighlight>ValidateAuthDummy { authorize($user) { return null; } }</syntaxhighlight>
<span id="stub"></span>
== 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.
<syntaxhighlight>ValidateAuthStub { authorize($user) { return true; } }</syntaxhighlight>
<span id="spies"></span>
== Spies ==
Are stubs that also record and log some information based on how they were called.
<span id="mock"></span>
== 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.
<syntaxhighlight>is_user_authorized() {
    $mockUser = new User().grantAuthorization();
    assert(true, isUserAuthorized($mockUser));
}</syntaxhighlight>
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.
<span id="fake"></span>
== Fake ==
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 {
    authorize($user) {
        return $user == ‘Bob’;
    }
}</syntaxhighlight>
  <!-- From bodyContent 1542994.mraw -->
  <!-- From bodyContent 1542994.mraw -->



Revision as of 11:33, 19 April 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’;
    }
}