Skip to content

Commit

Permalink
support custom error messages in verify
Browse files Browse the repository at this point in the history
  • Loading branch information
rezoled committed Jul 14, 2024
1 parent 07ef837 commit 65ffd91
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
28 changes: 16 additions & 12 deletions src/MethodStubVerificator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {MethodCallToStringConverter} from "./utils/MethodCallToStringConverter";
export class MethodStubVerificator<T> {
private methodCallToStringConverter: MethodCallToStringConverter = new MethodCallToStringConverter();

constructor(private methodToVerify: MethodToStub) {
constructor(private methodToVerify: MethodToStub, private errorMessage?: string) {

}

Expand Down Expand Up @@ -33,7 +33,7 @@ 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.throw(`${msg}
${this.actualCalls()}`);
}
}
Expand All @@ -42,15 +42,15 @@ ${this.actualCalls()}`);
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).`);
this.throw(`Expected "${methodToVerifyAsString}to be called at least ${value} time(s). But has been called ${allMatchingActions.length} time(s).`);
}
}

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).`);
this.throw(`Expected "${methodToVerifyAsString}to be called at least ${value} time(s). But has been called ${allMatchingActions.length} time(s).`);
}
}

Expand All @@ -63,14 +63,14 @@ ${this.actualCalls()}`);

if (firstMethodAction && secondMethodAction) {
if (!firstMethodAction.hasBeenCalledBefore(secondMethodAction)) {
throw new Error(`${errorBeginning}but has been called after.`);
this.throw(`${errorBeginning}but has been called after.`);
}
} else if (firstMethodAction && !secondMethodAction) {
throw new Error(`${errorBeginning}but ${secondMethodAsString}has never been called.`);
this.throw(`${errorBeginning}but ${secondMethodAsString}has never been called.`);
} else if (!firstMethodAction && secondMethodAction) {
throw new Error(`${errorBeginning}but ${mainMethodToVerifyAsString}has never been called.`);
this.throw(`${errorBeginning}but ${mainMethodToVerifyAsString}has never been called.`);
} else {
throw new Error(`${errorBeginning}but none of them has been called.`);
this.throw(`${errorBeginning}but none of them has been called.`);
}
}

Expand All @@ -83,14 +83,14 @@ ${this.actualCalls()}`);

if (firstMethodAction && secondMethodAction) {
if (firstMethodAction.hasBeenCalledBefore(secondMethodAction)) {
throw new Error(`${errorBeginning}but has been called before.`);
this.throw(`${errorBeginning}but has been called before.`);
}
} else if (firstMethodAction && !secondMethodAction) {
throw new Error(`${errorBeginning}but ${secondMethodAsString}has never been called.`);
this.throw(`${errorBeginning}but ${secondMethodAsString}has never been called.`);
} else if (!firstMethodAction && secondMethodAction) {
throw new Error(`${errorBeginning}but ${mainMethodToVerifyAsString}has never been called.`);
this.throw(`${errorBeginning}but ${mainMethodToVerifyAsString}has never been called.`);
} else {
throw new Error(`${errorBeginning}but none of them has been called.`);
this.throw(`${errorBeginning}but none of them has been called.`);
}
}

Expand All @@ -99,4 +99,8 @@ ${this.actualCalls()}`);
return `Actual calls:
${this.methodCallToStringConverter.convertActualCalls(calls).join("\n ")}`;
}

private throw(message: string) {
throw new Error(this.errorMessage ?? message);
}
}
4 changes: 2 additions & 2 deletions src/ts-mockito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export function mock<T>(clazz?: any): T {
return new Mocker(clazz).getMock();
}

export function verify<T>(method: T): MethodStubVerificator<T> {
return new MethodStubVerificator(method as any);
export function verify<T>(method: T, errorMessage?:string): MethodStubVerificator<T> {
return new MethodStubVerificator(method as any, errorMessage);
}

export function when<T>(method: Promise<T>): MethodStubSetter<Promise<T>, T, Error>;
Expand Down
17 changes: 17 additions & 0 deletions test/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,23 @@ cases.forEach(testData => {
expect(e.message).toContain(`sampleMethodWithObjectArguments({\"foo\":\"baz\"})`);
}
});

it("should describe error with the supplied error message", () => {
instance(mockedFoo).sampleMethodWithObjectArguments({foo: 'baz'});

try {
// when
verify(
mockedFoo.sampleMethodWithObjectArguments(deepEqual({foo: 'bar'})),
'sampleMethodWithObjectArguments should return baz!',
).once();

expect(true).toBe(false); // Above call should throw an exception
} catch (e) {
// then
expect(e.message).toContain('sampleMethodWithObjectArguments should return baz!');
}
});
});
});
});
Expand Down

0 comments on commit 65ffd91

Please sign in to comment.