Mocking
Say we have two functions, and multiply
, if we want to assert that
the multiply
function is called during execution of the square
function we
need a way to spy on the multiply
function. There are a few ways to achieve
this with Spies, one is to have the square
function take the multiply
multiply as a parameter.
This way, we can call square(multiply, value)
in the application code or wrap
a spy function around the multiply
function and call
square(multiplySpy, value)
in the testing code.
// https://deno.land/std@$STD_VERSION/testing/mock_examples/parameter_injection_test.ts
import {
assertSpyCall,
assertSpyCalls,
spy,
} from "https://deno.land/std@$STD_VERSION/testing/mock.ts";
import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
import {
multiply,
square,
} from "https://deno.land/std@$STD_VERSION/testing/mock_examples/parameter_injection.ts";
Deno.test("square calls multiply and returns results", () => {
const multiplySpy = spy(multiply);
assertEquals(square(multiplySpy, 5), 25);
// asserts that multiplySpy was called at least once and details about the first call.
assertSpyCall(multiplySpy, 0, {
args: [5, 5],
returned: 25,
});
// asserts that multiplySpy was only called once.
assertSpyCalls(multiplySpy, 1);
});
If you prefer not adding additional parameters for testing purposes only, you
can use spy to wrap a method on an object instead. In the following example, the
exported _internals
object has the multiply
function we want to call as a
method and the square
function calls _internals.multiply
instead of
multiply
.
// https://deno.land/std@$STD_VERSION/testing/mock_examples/internals_injection_test.ts
import {
assertSpyCall,
assertSpyCalls,
spy,
} from "https://deno.land/std@$STD_VERSION/testing/mock.ts";
import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
import {
_internals,
square,
} from "https://deno.land/std@$STD_VERSION/testing/mock_examples/internals_injection.ts";
const multiplySpy = spy(_internals, "multiply");
try {
assertEquals(square(5), 25);
} finally {
// unwraps the multiply method on the _internals object
multiplySpy.restore();
}
// asserts that multiplySpy was called at least once and details about the first call.
assertSpyCall(multiplySpy, 0, {
args: [5, 5],
returned: 25,
});
// asserts that multiplySpy was only called once.
assertSpyCalls(multiplySpy, 1);
});
One difference you may have noticed between these two examples is that in the
second we call the restore
method on multiplySpy
function. That is needed to
remove the spy wrapper from the _internals
object’s multiply
method. The
restore
method is called in a finally block to ensure that it is restored
whether or not the assertion in the try block is successful. The restore
method didn’t need to be called in the first example because the multiply
function was not modified in any way like the _internals
object was in the
second example.
Say we have two functions, randomMultiple
and randomInt
, if we want to
assert that randomInt
is called during execution of randomMultiple
we need a
way to spy on the randomInt
function. That could be done with either of the
spying techniques previously mentioned. To be able to verify that the
randomMultiple
function returns the value we expect it to for what randomInt
returns, the easiest way would be to replace the randomInt
function’s behavior
with more predictable behavior.
You could use the first spying technique to do that but that would require
adding a randomInt
parameter to the randomMultiple
function.
Say we want to verify it returns correct values for both negative and positive
random integers. We could easily do that with stubbing. The below example is
similar to the second spying technique example but instead of passing the call
through to the original randomInt
function, we are going to replace
randomInt
with a function that returns pre-defined values.
The mock module includes some helper functions to make creating common stubs
easy. The returnsNext
function takes an array of values we want it to return
on consecutive calls.
// https://deno.land/std@$STD_VERSION/testing/mock_examples/random_test.ts
import {
assertSpyCall,
assertSpyCalls,
returnsNext,
stub,
} from "https://deno.land/std@$STD_VERSION/testing/mock.ts";
import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
import {
_internals,
randomMultiple,
} from "https://deno.land/std@$STD_VERSION/testing/mock_examples/random.ts";
Deno.test("randomMultiple uses randomInt to generate random multiples between -10 and 10 times the value", () => {
const randomIntStub = stub(_internals, "randomInt", returnsNext([-3, 3]));
try {
assertEquals(randomMultiple(5), 15);
} finally {
// unwraps the randomInt method on the _internals object
randomIntStub.restore();
}
// asserts that randomIntStub was called at least once and details about the first call.
assertSpyCall(randomIntStub, 0, {
args: [-10, 10],
returned: -3,
});
// asserts that randomIntStub was called at least twice and details about the second call.
assertSpyCall(randomIntStub, 1, {
args: [-10, 10],
returned: 3,
});
// asserts that randomIntStub was only called twice.
assertSpyCalls(randomIntStub, 2);
});
Say we have a function that has time based behavior that we would like to test. With real time, that could cause tests to take much longer than they should. If you fake time, you could simulate how your function would behave over time starting from any point in time. Below is an example where we want to test that the callback is called every second.
// https://deno.land/std@$STD_VERSION/testing/mock_examples/interval_test.ts
import {
assertSpyCalls,
spy,
} from "https://deno.land/std@$STD_VERSION/testing/mock.ts";
import { FakeTime } from "https://deno.land/std@$STD_VERSION/testing/time.ts";
import { secondInterval } from "https://deno.land/std@$STD_VERSION/testing/mock_examples/interval.ts";
Deno.test("secondInterval calls callback every second and stops after being cleared", () => {
const time = new FakeTime();
try {
const cb = spy();
const intervalId = secondInterval(cb);
assertSpyCalls(cb, 0);
time.tick(500);
assertSpyCalls(cb, 0);
time.tick(500);
assertSpyCalls(cb, 1);
time.tick(3500);
assertSpyCalls(cb, 4);
clearInterval(intervalId);
time.tick(1000);
assertSpyCalls(cb, 4);
} finally {
time.restore();