Skip to content

Commit

Permalink
issue 48 - validating arguments count while verifying call count
Browse files Browse the repository at this point in the history
  • Loading branch information
NagRock committed Oct 4, 2017
1 parent a14a3ef commit 9822df4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/MethodAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export class MethodAction {
}

public isApplicable(methodName: string, matchers: Matcher[]): boolean {
if (this.methodName !== methodName) {
const methodNameMatch = this.methodName === methodName;
const argumentsCountMatch = this.args.length == matchers.length;
if (!methodNameMatch || !argumentsCountMatch) {
return false;
}
let allValid = true;
Expand Down
6 changes: 5 additions & 1 deletion test/utils/Foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ export class Foo {
public sampleMethodWithOptionalArgument(a: number, b?: number): number {
return a + b;
}
}

public sampleMethodWithTwoOptionalArguments(a?: number, b?:number): number {
return a + b;
}
}
31 changes: 31 additions & 0 deletions test/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,37 @@ describe("verifying mocked object", () => {
});
});

describe("and arguments are optional", () => {
describe("but just first one is given", () => {
it("throws error", () => {
// given
foo.sampleMethodWithTwoOptionalArguments(5);

// when
let error;
try {
verify(mockedFoo.sampleMethodWithTwoOptionalArguments(5, 6)).called();
} catch (e) {
error = e;
}
expect(error).toBeTruthy();
});
});
});

describe("and arguments are optional", () => {
it("verifies call count correctly", () => {
// given
foo.sampleMethodWithTwoOptionalArguments(5);
foo.sampleMethodWithTwoOptionalArguments(5);
foo.sampleMethodWithTwoOptionalArguments(5, 6);

// when
verify(mockedFoo.sampleMethodWithTwoOptionalArguments(5)).twice();
verify(mockedFoo.sampleMethodWithTwoOptionalArguments(5, 6)).once();
});
});

describe("but two has occurred", () => {
it("throws error", () => {
// given
Expand Down

0 comments on commit 9822df4

Please sign in to comment.