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

Better error messages when failing call verification #4

Merged
merged 1 commit into from
Dec 15, 2019
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
16 changes: 12 additions & 4 deletions src/MethodStubVerificator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const localSetTimeout = setTimeout;
export class MethodStubVerificator<T> {
private methodCallToStringConverter: MethodCallToStringConverter = new MethodCallToStringConverter();

private actualCalls() {
const calls = this.methodToVerify.mocker.getActionsByName(this.methodToVerify.methodName);
return 'Actual calls:\n ' + this.methodCallToStringConverter.convertActualCalls(calls).join('\n ');
}

constructor(private methodToVerify: MethodToStub) {
methodToVerify.watcher.invoked();
}
Expand Down Expand Up @@ -34,24 +39,27 @@ export class MethodStubVerificator<T> {
public times(value: number): void {
const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.methodName, this.methodToVerify.matchers);
if (value !== allMatchingActions.length) {
const methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
throw new Error(`Expected "${methodToVerifyAsString}to be called ${value} time(s). But has been called ${allMatchingActions.length} time(s).`);
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 + '\n' + this.actualCalls());
}
}

public atLeast(value: number): void {
const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.methodName, 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.methodName, 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
5 changes: 5 additions & 0 deletions src/utils/MethodCallToStringConverter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Matcher} from "../matcher/type/Matcher";
import {MethodToStub} from "../MethodToStub";
import { MethodAction } from "../MethodAction";

export class MethodCallToStringConverter {
public convert(method: MethodToStub): string {
Expand All @@ -8,4 +9,8 @@ export class MethodCallToStringConverter {
const stringifiedMatchers = method.matchers.map((matcher: Matcher) => matcher.toString()).join(", ");
return `${method.methodName}(${stringifiedMatchers})" `;
}

public convertActualCalls(calls: MethodAction[]): string[] {
return calls.map(call => call.methodName + '(' + call.args.map(arg => arg.toString()).join(', ') + ')');
}
}
18 changes: 18 additions & 0 deletions test/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,24 @@ describe("verifying mocked object", () => {
verify(mockedFoo.getBar()).once();
});
});

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

try {
// when
verify(mockedFoo.getStringById(1)).once();

expect(true).toBe(false); // Above call should throw an exception
} catch (e) {
// then
expect(e.message).toMatch(/getStringById\(strictEqual\(1\)\)/);
expect(e.message).toMatch(/getStringById\(2\)/);
}
});
});
});

function verifyCallCountErrorMessage(error, expectedCallCount, receivedCallCount): void {
Expand Down