Skip to content

Commit 854041c

Browse files
committed
Added mock resetting functionality
1 parent 8507d9e commit 854041c

File tree

6 files changed

+155
-14
lines changed

6 files changed

+155
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-mockito",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "",
55
"main": "lib/ts-mockito.js",
66
"typings": "lib/ts-mockito",

src/MethodStubVerificator.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,32 @@ export class MethodStubVerificator<T> {
2929
}
3030

3131
public times(value: number): void {
32-
let allMatchingActions = this.methodToVerify.mock.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
32+
let allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
3333
if (value !== allMatchingActions.length) {
3434
let methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
3535
throw new Error('Expected "' + methodToVerifyAsString + 'to be called ' + value + ' time(s). But has been called ' + allMatchingActions.length + ' time(s).');
3636
}
3737
}
3838

3939
public atLeast(value: number): void {
40-
let allMatchingActions = this.methodToVerify.mock.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
40+
let allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
4141
if (value > allMatchingActions.length) {
4242
let methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
4343
throw new Error('Expected "' + methodToVerifyAsString + 'to be called at least ' + value + ' time(s). But has been called ' + allMatchingActions.length + ' time(s).');
4444
}
4545
}
4646

4747
public atMost(value: number): void {
48-
let allMatchingActions = this.methodToVerify.mock.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
48+
let allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
4949
if (value < allMatchingActions.length) {
5050
let methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
5151
throw new Error('Expected "' + methodToVerifyAsString + 'to be called at least ' + value + ' time(s). But has been called ' + allMatchingActions.length + ' time(s).');
5252
}
5353
}
5454

5555
public calledBefore(method: any): void {
56-
const firstMethodAction = this.methodToVerify.mock.getFirstMatchingAction(this.methodToVerify.name, this.methodToVerify.matchers);
57-
const secondMethodAction = method.mock.getFirstMatchingAction(method.name, method.matchers);
56+
const firstMethodAction = this.methodToVerify.mocker.getFirstMatchingAction(this.methodToVerify.name, this.methodToVerify.matchers);
57+
const secondMethodAction = method.mocker.getFirstMatchingAction(method.name, method.matchers);
5858
let mainMethodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
5959
let secondMethodAsString = this.methodCallToStringConverter.convert(method);
6060
let errorBeginning = 'Expected "' + mainMethodToVerifyAsString + 'to be called before ' + secondMethodAsString;
@@ -73,8 +73,8 @@ export class MethodStubVerificator<T> {
7373
}
7474

7575
public calledAfter(method: any): void {
76-
const firstMethodAction = this.methodToVerify.mock.getFirstMatchingAction(this.methodToVerify.name, this.methodToVerify.matchers);
77-
const secondMethodAction = method.mock.getFirstMatchingAction(method.name, method.matchers);
76+
const firstMethodAction = this.methodToVerify.mocker.getFirstMatchingAction(this.methodToVerify.name, this.methodToVerify.matchers);
77+
const secondMethodAction = method.mocker.getFirstMatchingAction(method.name, method.matchers);
7878
let mainMethodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
7979
let secondMethodAsString = this.methodCallToStringConverter.convert(method);
8080
let errorBeginning = 'Expected "' + mainMethodToVerifyAsString + 'to be called after ' + secondMethodAsString;

src/MethodToStub.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {MethodStubCollection} from './MethodStubCollection';
22
import {Matcher} from './matcher/type/Matcher';
3-
import {Mock} from './Mock';
3+
import {Mocker} from './Mock';
44

55
export class MethodToStub {
66
constructor(public methodStubCollection: MethodStubCollection,
77
public matchers: Array<Matcher>,
8-
public mock: Mock,
8+
public mocker: Mocker,
99
public name: string) {
1010
}
1111
}

src/Mock.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import {MethodStub} from "./stub/MethodStub";
77
import {RedundantMethodNameInCodeFinder} from "./utils/RedundantMethodNameInCodeFinder";
88
import {strictEqual} from "./ts-mockito";
99

10-
export class Mock {
10+
export class Mocker {
1111
private methodStubCollections: any = {};
1212
private methodActions: Array<MethodAction> = [];
1313
private mock: any = {};
1414
private instance: any = {};
1515

1616
constructor(private clazz: any) {
1717
this.mock.__tsmockitoInstance = this.instance;
18+
this.mock.__tsmockitoMocker = this;
1819
this.createMethodStubsFromPrototypeOwnPropertyNames();
1920
this.createMethodStubsFromPrototypeKeys();
2021
this.createMethodStubsFromClassCode();
@@ -29,6 +30,10 @@ export class Mock {
2930
return this.mock;
3031
}
3132

33+
public reset(): void {
34+
this.methodActions = [];
35+
}
36+
3237
public getAllMatchingActions(methodName: string, matchers: Array<Matcher>): Array<MethodAction> {
3338
let result: Array<MethodAction> = [];
3439

src/ts-mockito.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Mock} from "./Mock";
1+
import {Mocker} from "./Mock";
22
import {MethodStubVerificator} from "./MethodStubVerificator";
33
import {MethodStubSetter} from "./MethodStubSetter";
44
import {AnyNumberMatcher} from "./matcher/type/AnyNumberMatcher";
@@ -11,8 +11,8 @@ import {Matcher} from "./matcher/type/Matcher";
1111
import {StrictEqualMatcher} from "./matcher/type/StrictEqualMatcher";
1212
export {Captor} from "./Captor";
1313

14-
export function mock<T>(clazz: {new(...args: any[]): T; }): T {
15-
return new Mock(clazz).getMock();
14+
export function mock<T>(clazz: {new(...args: any[]): T;}): T {
15+
return new Mocker(clazz).getMock();
1616
}
1717

1818
export function verify<T>(method: T): MethodStubVerificator<T> {
@@ -27,6 +27,10 @@ export function instance<T>(mock: T): T {
2727
return (mock as any).__tsmockitoInstance as T;
2828
}
2929

30+
export function reset<T>(mock: T): void {
31+
(mock as any).__tsmockitoMocker.reset();
32+
}
33+
3034
export function anyNumber(): any {
3135
return new AnyNumberMatcher() as any;
3236
}

test/reseting.spec.ts

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import {Foo} from "./utils/Foo";
2+
import {mock, instance, verify, reset, anything} from "../src/ts-mockito";
3+
4+
describe("resetting mocked object", () => {
5+
let mockedFoo: Foo;
6+
let foo: Foo;
7+
8+
beforeEach(() => {
9+
mockedFoo = mock(Foo);
10+
foo = instance(mockedFoo);
11+
});
12+
13+
describe("when method has been called once", () => {
14+
describe("but later stub has been reset", () => {
15+
it("shows that never been called", () => {
16+
// given
17+
foo.getBar();
18+
verify(mockedFoo.getBar()).once();
19+
20+
// when
21+
reset(mockedFoo);
22+
23+
// then
24+
verify(mockedFoo.getBar()).never();
25+
});
26+
});
27+
});
28+
29+
describe("when method has been called thrice", () => {
30+
describe("but later stub has been reset", () => {
31+
it("shows that never been called", () => {
32+
// given
33+
foo.getBar();
34+
foo.getBar();
35+
foo.getBar();
36+
verify(mockedFoo.getBar()).thrice();
37+
38+
// when
39+
reset(mockedFoo);
40+
41+
// then
42+
verify(mockedFoo.getBar()).never();
43+
});
44+
});
45+
});
46+
47+
describe("when method has been called with arguments twice", () => {
48+
describe("but later stub has been reset", () => {
49+
it("shows that never been called", () => {
50+
// given
51+
foo.sumTwoNumbers(2, 3);
52+
foo.sumTwoNumbers(2, 3);
53+
verify(mockedFoo.sumTwoNumbers(2, 3)).twice();
54+
55+
// when
56+
reset(mockedFoo);
57+
58+
// then
59+
verify(mockedFoo.sumTwoNumbers(2, 3)).never();
60+
verify(mockedFoo.sumTwoNumbers(anything(), anything())).never();
61+
});
62+
});
63+
});
64+
65+
describe("when two different methods has been called twice", () => {
66+
describe("but later stub has been reset", () => {
67+
it("shows that never been called", () => {
68+
// given
69+
foo.getBar();
70+
foo.getBar();
71+
foo.sumTwoNumbers(2, 3);
72+
foo.sumTwoNumbers(2, 3);
73+
verify(mockedFoo.getBar()).twice();
74+
verify(mockedFoo.sumTwoNumbers(2, 3)).twice();
75+
76+
// when
77+
reset(mockedFoo);
78+
79+
// then
80+
verify(mockedFoo.getBar()).never();
81+
verify(mockedFoo.sumTwoNumbers(2, 3)).never();
82+
verify(mockedFoo.sumTwoNumbers(anything(), anything())).never();
83+
});
84+
});
85+
});
86+
87+
describe("when two different methods has been called", () => {
88+
describe("but later stub has been reset", () => {
89+
it("throws exception with information that methods has not been called", () => {
90+
// given
91+
foo.getBar();
92+
foo.sumTwoNumbers(2, 3);
93+
verify(mockedFoo.getBar()).calledBefore(mockedFoo.sumTwoNumbers(2, 3));
94+
95+
// when
96+
reset(mockedFoo);
97+
98+
// then
99+
try {
100+
verify(mockedFoo.getBar()).calledBefore(mockedFoo.sumTwoNumbers(2, 3));
101+
fail();
102+
} catch (e) {
103+
expect(e.message).toContain('to be called before');
104+
expect(e.message).toContain('but none of them has been called');
105+
}
106+
});
107+
});
108+
});
109+
110+
describe("when two different methods has been after", () => {
111+
describe("but later stub has been reset", () => {
112+
it("throws exception with information that methods has not been called", () => {
113+
// given
114+
foo.getBar();
115+
foo.sumTwoNumbers(2, 3);
116+
verify(mockedFoo.sumTwoNumbers(2, 3)).calledAfter(mockedFoo.getBar());
117+
118+
// when
119+
reset(mockedFoo);
120+
121+
// then
122+
try {
123+
verify(mockedFoo.sumTwoNumbers(2, 3)).calledAfter(mockedFoo.getBar());
124+
fail();
125+
} catch (e) {
126+
expect(e.message).toContain('to be called after');
127+
expect(e.message).toContain('but none of them has been called');
128+
}
129+
});
130+
});
131+
});
132+
});

0 commit comments

Comments
 (0)