Skip to content
Open
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
12 changes: 6 additions & 6 deletions src/MethodStubVerificator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,25 @@ export class MethodStubVerificator<T> {
if (value !== allMatchingActions.length) {
const methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
const msg = `Expected "${methodToVerifyAsString}to be called ${value} time(s). But has been called ${allMatchingActions.length} time(s).`;
throw new Error(`${msg}
${this.actualCalls()}`);
throw new Error(`${msg}\n${this.actualCalls()}`);
}
}

public atLeast(value: number): void {
const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
if (value > allMatchingActions.length) {
const methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
throw new Error(`Expected "${methodToVerifyAsString}to be called at least ${value} time(s). But has been called ${allMatchingActions.length} time(s).`);
const msg = `Expected "${methodToVerifyAsString}to be called at least ${value} time(s). But has been called ${allMatchingActions.length} time(s).`;
throw new Error(`${msg}\n${this.actualCalls()}`);
}
}

public atMost(value: number): void {
const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
if (value < allMatchingActions.length) {
const methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
throw new Error(`Expected "${methodToVerifyAsString}to be called at least ${value} time(s). But has been called ${allMatchingActions.length} time(s).`);
const msg = `Expected "${methodToVerifyAsString}to be called at most ${value} time(s). But has been called ${allMatchingActions.length} time(s).`;
throw new Error(`${msg}\n${this.actualCalls()}`);
}
}

Expand Down Expand Up @@ -96,7 +97,6 @@ ${this.actualCalls()}`);

private actualCalls() {
const calls = this.methodToVerify.mocker.getActionsByName(this.methodToVerify.name);
return `Actual calls:
${this.methodCallToStringConverter.convertActualCalls(calls).join("\n ")}`;
return `Actual calls:\n ${this.methodCallToStringConverter.convertActualCalls(calls).join("\n ")}`;
}
}
130 changes: 109 additions & 21 deletions test/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,36 +786,113 @@ cases.forEach(testData => {
});

describe("matcher error messages", () => {
it("should describe expected method call", () => {
it("should describe expected method call: called()", () => {
instance(mockedFoo).getStringById(2);

try {
// when
expectException(() => {
verify(mockedFoo.getStringById(1)).called();
}, errMsg => {
expect(errMsg).toContain("Expected \"getStringById(strictEqual(1))\" to be called at least 1 time(s). But has been called 0 time(s).\n");
expect(errMsg).toContain("Actual calls:\n");
expect(errMsg).toContain("getStringById(2)");
});
});

it("should describe expected method call: never()", () => {
instance(mockedFoo).getStringById(1);

expectException(() => {
verify(mockedFoo.getStringById(1)).never();
}, errMsg => {
expect(errMsg).toContain("Expected \"getStringById(strictEqual(1))\" to be called 0 time(s). But has been called 1 time(s).\n");
expect(errMsg).toContain("Actual calls:\n");
expect(errMsg).toContain("getStringById(1)");
});
});

it("should describe expected method call: once()", () => {
instance(mockedFoo).getStringById(2);

expectException(() => {
verify(mockedFoo.getStringById(1)).once();
}, errMsg => {
expect(errMsg).toContain("Expected \"getStringById(strictEqual(1))\" to be called 1 time(s). But has been called 0 time(s).\n");
expect(errMsg).toContain("Actual calls:\n");
expect(errMsg).toContain("getStringById(2)");
});
});

expect(true).toBe(false); // Above call should throw an exception
} catch (e) {
// then
expect(e.message).toContain("Expected \"getStringById(strictEqual(1))\" to be called 1 time(s). But has been called 0 time(s).\n");
expect(e.message).toContain("Actual calls:\n");
expect(e.message).toContain("getStringById(2)");
}
it("should describe expected method call: twice()", () => {
instance(mockedFoo).getStringById(2);

expectException(() => {
verify(mockedFoo.getStringById(1)).twice();
}, errMsg => {
expect(errMsg).toContain("Expected \"getStringById(strictEqual(1))\" to be called 2 time(s). But has been called 0 time(s).\n");
expect(errMsg).toContain("Actual calls:\n");
expect(errMsg).toContain("getStringById(2)");
});
});

it("should describe expected method call: thrice()", () => {
instance(mockedFoo).getStringById(2);

expectException(() => {
verify(mockedFoo.getStringById(1)).thrice();
}, errMsg => {
expect(errMsg).toContain("Expected \"getStringById(strictEqual(1))\" to be called 3 time(s). But has been called 0 time(s).\n");
expect(errMsg).toContain("Actual calls:\n");
expect(errMsg).toContain("getStringById(2)");
});
});

it("should describe expected method call: times(2)", () => {
instance(mockedFoo).getStringById(2);

expectException(() => {
verify(mockedFoo.getStringById(1)).times(2);
}, errMsg => {
expect(errMsg).toContain("Expected \"getStringById(strictEqual(1))\" to be called 2 time(s). But has been called 0 time(s).\n");
expect(errMsg).toContain("Actual calls:\n");
expect(errMsg).toContain("getStringById(2)");
});
});

it("should describe expected method call: atLeast(2)", () => {
instance(mockedFoo).getStringById(1);

expectException(() => {
verify(mockedFoo.getStringById(1)).atLeast(2);
}, errMsg => {
expect(errMsg).toContain("Expected \"getStringById(strictEqual(1))\" to be called at least 2 time(s). But has been called 1 time(s).\n");
expect(errMsg).toContain("Actual calls:\n");
expect(errMsg).toContain("getStringById(1)");
});
});

it("should describe expected method call: atMost(1)", () => {
instance(mockedFoo).getStringById(1);
instance(mockedFoo).getStringById(1);

expectException(() => {
verify(mockedFoo.getStringById(1)).atMost(1);
}, errMsg => {
expect(errMsg).toContain("Expected \"getStringById(strictEqual(1))\" to be called at most 1 time(s). But has been called 2 time(s).\n");
expect(errMsg).toContain("Actual calls:\n");
expect(errMsg).toContain("getStringById(1)\n getStringById(1)");
});
});

it("should describe expected method call objects", () => {
instance(mockedFoo).sampleMethodWithObjectArguments({foo: 'baz'});

try {
// when
expectException(() => {
verify(mockedFoo.sampleMethodWithObjectArguments(deepEqual({foo: 'bar'}))).once();

expect(true).toBe(false); // Above call should throw an exception
} catch (e) {
// then
expect(e.message).toContain('Expected "sampleMethodWithObjectArguments(deepEqual({\"foo\":\"bar\"}))" to be called 1 time(s). But has been called 0 time(s).\n');
expect(e.message).toContain("Actual calls:\n");
expect(e.message).toContain(`sampleMethodWithObjectArguments({\"foo\":\"baz\"})`);
}
}, errMsg => {
expect(errMsg).toContain('Expected "sampleMethodWithObjectArguments(deepEqual({\"foo\":\"bar\"}))" to be called 1 time(s). But has been called 0 time(s).\n');
expect(errMsg).toContain("Actual calls:\n");
expect(errMsg).toContain(`sampleMethodWithObjectArguments({\"foo\":\"baz\"})`);
});
});
});

Expand Down Expand Up @@ -849,7 +926,7 @@ cases.forEach(testData => {
"special_chars-@?": () => {
// do nothing
}
}
};

it("should mock class", () => {
const mocked = mock(TestClass);
Expand All @@ -868,3 +945,14 @@ function verifyCallCountErrorMessage(error, expectedCallCount, receivedCallCount
expect(error.message).toContain(`${expectedCallCount} time(s). But`);
expect(error.message).toContain(`has been called ${receivedCallCount} time(s)`);
}

const expectException = (doSomethingThatRaises: () => void, checkExceptionMessage: (errMsg: string) => void) => {
try {
// when
doSomethingThatRaises();
fail("Expected an exception, but nothing was raised");
} catch (e) {
// then
checkExceptionMessage(e.message);
}
};