How do you isolate-test a function when it has dependency(s) on it?

The gist of isolated test means we are not going to pass-in the real dependency. Like stunt doubles, we need a ‘double’ on the dependencies, namely test doubles.

Mock . Mocking a dependency is merely to install a register on the dependency. A mocked dependency registers any calls to it: # of executions, and parameters.

. Mocking a dependency is merely to install a register on the dependency. A mocked dependency registers any calls to it: # of executions, and parameters. Stub. Stubbing a dependency is to return canned (hardcoded) results from the dependency.

These test doubles are designed for one sole purpose, to ensure our function interacts/contacts (act, react) correctly with the dependencies (without actually interact with it)

function (dependency) {

......

.........

...... result = dependency(parameter1, parameter2, ....); ....

..... return;

}

Illustrated in the stripped-down algorithms.

We want to verify that function acted with correct parameters, executed the right amount (once, twice, thrice) We want to verify that function reacts correctly based on the canned results, i.e: by asserting on the state of the function.

References