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

Custom error message to verify #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ verify(mockedFoo.getBar(3)).called();
verify(mockedFoo.getBar(anything())).called();
```

#### Throwing custom test error message
The verify assertion will throw a test error with predefined error template.
If you need to throw a different error message in the test you can supply the ```verify``` command with an optional error message parameter:
``` typescript
verify(mockedFoo.getBar(), 'getBar should have been called, but didnt!').called();
```

### Stubbing method calls

``` typescript
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typestrong/ts-mockito",
"version": "2.6.7",
"version": "2.6.8",
"description": "Mocking library for TypeScript",
"main": "lib/ts-mockito.js",
"typings": "lib/ts-mockito",
Expand Down
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
Loading