Stub
import { Stub } from "jsr:@ende/testing/mock";interface
interface Stub<Self = any, Args extends unknown[] = any[], Return = any> extends MethodSpy<Self, Args, Return>An instance method replacement that records all calls made to it.
Properties
fake
fake: (this: Self, ...args: Args) => ReturnThe function that is used instead of the original.
stub
import { stub } from "jsr:@ende/testing/mock";function
function stub<Self, Prop extends keyof Self>(self: Self, property: Prop): Stub<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>>Replaces an instance method with a Stub with empty implementation.
self: SelfThe instance to replace a method of.
property: PropThe property of the instance to replace.
Returns
The stub function which replaced the original.
Example: Usage
import { stub, assertSpyCalls } from "@ende/testing/mock";
const obj = {
method() {
// some inconventient feature for testing
},
};
const methodStub = stub(obj, "method");
for (const _ of Array(5)) {
obj.method();
}
assertSpyCalls(methodStub, 5);
function
function stub<Self, Prop extends keyof Self>(self: Self, property: Prop, func: (this: Self, ...args: GetParametersFromProp<Self, Prop>) => GetReturnFromProp<Self, Prop>): Stub<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>>Replaces an instance method with a Stub with the given implementation.
self: SelfThe instance to replace a method of.
property: PropThe property of the instance to replace.
func: (this: Self, ...args: GetParametersFromProp<Self, Prop>) => GetReturnFromProp<Self, Prop>The fake implementation of the function.
Returns
The stub function which replaced the original.
Example: Usage
import { stub } from "@ende/testing/mock";
import { assertEquals } from "@ende/assert";
const obj = {
method(): number {
return Math.random();
},
};
const methodStub = stub(obj, "method", () => 0.5);
assertEquals(obj.method(), 0.5);
function
function stub<Self, Args extends unknown[], Return>(self: Self, property: keyof Self, func?: (this: Self, ...args: Args) => Return): Stub<Self, Args, Return>