Skip to content

Commit 430e566

Browse files
author
Johan Blumenberg
committed
Add resetStubs()
1 parent 463e569 commit 430e566

File tree

4 files changed

+165
-2
lines changed

4 files changed

+165
-2
lines changed

src/Mock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ export class Mocker {
8383
this.methodActions = [];
8484
}
8585

86+
public resetStubs(): void {
87+
this.methodStubCollections = {};
88+
}
89+
8690
public resetCalls(): void {
8791
this.methodActions = [];
8892
}

src/ts-mockito.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ export function reset<T>(mockedValue: T): void {
125125
(mockedValue as any).__tsmockitoMocker.reset();
126126
}
127127

128+
export function resetStubs<T>(mockedValue: T): void {
129+
(mockedValue as any).__tsmockitoMocker.resetStubs();
130+
}
131+
128132
export function resetCalls<T>(mockedValue: T): void {
129133
(mockedValue as any).__tsmockitoMocker.resetCalls();
130134
}
@@ -254,6 +258,7 @@ export default {
254258
instance,
255259
capture,
256260
reset,
261+
resetStubs,
257262
resetCalls,
258263
anyOfClass,
259264
anyFunction,

test/reseting.spec.ts

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {anything, instance, mock, reset, verify, when} from "../src/ts-mockito";
1+
import {anything, instance, mock, reset, resetStubs, verify, when} from "../src/ts-mockito";
22
import {Foo} from "./utils/Foo";
33

44
describe("resetting mocked object", () => {
@@ -163,3 +163,101 @@ describe("resetting mocked object", () => {
163163
});
164164
});
165165
});
166+
167+
describe("resetting mocked object stubs", () => {
168+
let mockedFoo: Foo;
169+
let foo: Foo;
170+
171+
beforeEach(() => {
172+
mockedFoo = mock(Foo);
173+
foo = instance(mockedFoo);
174+
});
175+
176+
describe("when method has been called once", () => {
177+
describe("but later stub has been reset", () => {
178+
it("shows that still been called", () => {
179+
// given
180+
foo.getBar();
181+
verify(mockedFoo.getBar()).once();
182+
183+
// when
184+
resetStubs(mockedFoo);
185+
186+
// then
187+
verify(mockedFoo.getBar()).once();
188+
});
189+
});
190+
});
191+
192+
describe("when two different methods has been called twice", () => {
193+
describe("but later stub has been reset", () => {
194+
it("shows that still been called", () => {
195+
// given
196+
foo.getBar();
197+
foo.getBar();
198+
foo.sumTwoNumbers(2, 3);
199+
foo.sumTwoNumbers(2, 3);
200+
verify(mockedFoo.getBar()).twice();
201+
verify(mockedFoo.sumTwoNumbers(2, 3)).twice();
202+
203+
// when
204+
resetStubs(mockedFoo);
205+
206+
// then
207+
verify(mockedFoo.getBar()).twice();
208+
verify(mockedFoo.sumTwoNumbers(2, 3)).twice();
209+
verify(mockedFoo.sumTwoNumbers(anything(), anything())).twice();
210+
});
211+
});
212+
});
213+
214+
describe("when two different methods has been called", () => {
215+
describe("but later stub has been reset", () => {
216+
it("throws exception with information that methods has still been called", () => {
217+
// given
218+
foo.getBar();
219+
foo.sumTwoNumbers(2, 3);
220+
verify(mockedFoo.getBar()).calledBefore(mockedFoo.sumTwoNumbers(2, 3));
221+
222+
// when
223+
resetStubs(mockedFoo);
224+
225+
// then
226+
verify(mockedFoo.getBar()).calledBefore(mockedFoo.sumTwoNumbers(2, 3));
227+
});
228+
});
229+
});
230+
231+
describe("when object has been stubbed", () => {
232+
describe("but later stub has been reset", () => {
233+
it("should reset configured stubs", () => {
234+
// given
235+
when(mockedFoo.convertNumberToString(3)).thenReturn("three");
236+
resetStubs(mockedFoo);
237+
238+
// when
239+
const result: string = foo.convertNumberToString(3);
240+
241+
// then
242+
expect(result).toBeNull();
243+
});
244+
});
245+
});
246+
247+
describe("when object has been stubbed", () => {
248+
describe("but later stub has been reset", () => {
249+
it("should be able to setup new stubs", () => {
250+
// given
251+
when(mockedFoo.convertNumberToString(3)).thenReturn("three");
252+
resetStubs(mockedFoo);
253+
when(mockedFoo.convertNumberToString(3)).thenReturn("newStubbedValue");
254+
255+
// when
256+
const result: string = foo.convertNumberToString(3);
257+
258+
// then
259+
expect(result).toEqual("newStubbedValue");
260+
});
261+
});
262+
});
263+
});

test/spy.spec.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {capture, reset, spy, verify, when} from "../src/ts-mockito";
1+
import {capture, reset, resetStubs, spy, verify, when} from "../src/ts-mockito";
22

33
describe("spying on a real object", () => {
44
class Real {
@@ -223,6 +223,62 @@ describe("spying on a real object", () => {
223223
});
224224
});
225225

226+
describe("resetting stubs", () => {
227+
it("restores a call to the real method", () => {
228+
// given
229+
const foo = new Real();
230+
const spiedFoo = spy(foo);
231+
232+
// when
233+
when(spiedFoo.bar()).thenReturn(3);
234+
resetStubs(spiedFoo);
235+
236+
// then
237+
expect(foo.bar()).toBe(2);
238+
});
239+
240+
it("set new stub calls to the real method", () => {
241+
// given
242+
const foo = new Real();
243+
const spiedFoo = spy(foo);
244+
245+
// when
246+
when(spiedFoo.bar()).thenReturn(3);
247+
resetStubs(spiedFoo);
248+
when(spiedFoo.bar()).thenReturn(4);
249+
250+
// then
251+
expect(foo.bar()).toBe(4);
252+
});
253+
254+
it("restores getter properties", () => {
255+
// given
256+
const foo = new Real();
257+
const spiedFoo = spy(foo);
258+
259+
// when
260+
when(spiedFoo.baz).thenReturn(42);
261+
resetStubs(spiedFoo);
262+
263+
// then
264+
expect(foo.baz).toBe(3);
265+
});
266+
267+
it("set new stub properties", () => {
268+
// given
269+
const foo = new Real();
270+
const spiedFoo = spy(foo);
271+
272+
// when
273+
when(spiedFoo.baz).thenReturn(42);
274+
resetStubs(spiedFoo);
275+
when(spiedFoo.baz).thenReturn(43);
276+
277+
// then
278+
expect(foo.baz).toBe(43);
279+
});
280+
});
281+
226282
describe("spying on object which doesn't inherit from anything", () => {
227283
let bareObject;
228284

0 commit comments

Comments
 (0)