Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #28 and #21 mock method arguments #77

Merged
merged 7 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/mock_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class MockBuilder<T> {
public create(): Mocked<T> {
// deno-lint-ignore no-explicit-any
const mock: Mocked<any> = {
calls: {},
methods: {},
is_mock: true,
};
const original = new this.constructor_fn(...this.constructor_args);
Expand Down Expand Up @@ -74,14 +74,39 @@ export class MockBuilder<T> {
];

if (nativeMethods.indexOf(method) == -1) {
if (!mock.calls[method]) {
mock.calls[method] = 0;

// Define an object to track data for assertions
if (!mock.methods[method]) {
mock.methods[method] = {
num_calls: 0,
wasCalledTimes: function (input: number) {
return input == this.num_calls;
}
}
}
mock[method] = function () {
mock.calls[method]++;
return (original[method as keyof T] as unknown as (

mock[method] = function (...args: unknown[]) {

// Count how many times this method was called
mock.methods[method].num_calls++;

// Track the last arguments that this method was called with
mock.methods[method].wasLastCalledWith = function (input: unknown[]) {
const i = JSON.stringify(input);
const a = JSON.stringify(args);
return i == a;
}

const ret = (original[method as keyof T] as unknown as (
...params: unknown[]
) => unknown)();
) => unknown)(...args);

// Track what this method last returned
mock.methods[method].lastReturned = function (input: unknown) {
return input == ret;
}

return ret;
};
} else {
// copy nativeMethod directly without mocking
Expand Down
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ export type TConstructorFunction<T> = {
export type Constructor<T extends unknown> = new (...args: any[]) => T;

export type Mocked<T> = T & {
calls: { [k in keyof T]: T[k] extends () => void ? number : never };
methods: {
[k: string]: {
num_calls: number;
lastReturned: (input: unknown) => boolean;
wasLastCalledWith: (...input: unknown[]) => boolean;
wasCalledTimes: (input: number) => boolean;
}
}
is_mock: true;
};

Expand Down
35 changes: 32 additions & 3 deletions tests/integration/mock_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,46 @@ Rhum.testPlan(() => {
);
});

Rhum.testCase("call count for outside function is increased", () => {
Rhum.testCase("wasCalledTimes() tracks external function calls", () => {
const mockMathService = Rhum
.mock(MathService)
.create();
const mockTestObject = Rhum
.mock(TestObject)
.withConstructorArgs("has mocked math service", mockMathService)
.create();
Rhum.asserts.assertEquals(mockMathService.calls.add, 0);
Rhum.asserts.assert(mockMathService.methods.add.wasCalledTimes(0));
mockTestObject.sum(1, 1);
Rhum.asserts.assertEquals(mockMathService.calls.add, 1);
Rhum.asserts.assert(mockMathService.methods.add.wasCalledTimes(1));
});

Rhum.testCase("lastCalledWith() returns the expected args", () => {
const mockMathService = Rhum
.mock(MathService)
.create();
mockMathService.add(1, 1);
Rhum.asserts.assert(
mockMathService.methods.add.wasLastCalledWith([1, 1])
);
mockMathService.add(2, 1);
Rhum.asserts.assert(
mockMathService.methods.add.wasLastCalledWith([2, 1])
);
});

Rhum.testCase("lastReturned() returns the expected return value", () => {
const mockMathService = Rhum
.mock(MathService)
.create();
mockMathService.add(1, 1);
Rhum.asserts.assert(
mockMathService.methods.add.lastReturned(2)
);
mockMathService.add(2, 1);
Rhum.asserts.assert(
mockMathService.methods.add.lastReturned(3)
);
});

});
});