Skip to content

Commit

Permalink
Added mock resetting functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
NagRock committed Jan 5, 2017
1 parent 8507d9e commit 854041c
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-mockito",
"version": "1.1.1",
"version": "1.1.2",
"description": "",
"main": "lib/ts-mockito.js",
"typings": "lib/ts-mockito",
Expand Down
14 changes: 7 additions & 7 deletions src/MethodStubVerificator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ export class MethodStubVerificator<T> {
}

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

public atLeast(value: number): void {
let allMatchingActions = this.methodToVerify.mock.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
let allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
if (value > allMatchingActions.length) {
let 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).');
}
}

public atMost(value: number): void {
let allMatchingActions = this.methodToVerify.mock.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
let allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
if (value < allMatchingActions.length) {
let 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).');
}
}

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

public calledAfter(method: any): void {
const firstMethodAction = this.methodToVerify.mock.getFirstMatchingAction(this.methodToVerify.name, this.methodToVerify.matchers);
const secondMethodAction = method.mock.getFirstMatchingAction(method.name, method.matchers);
const firstMethodAction = this.methodToVerify.mocker.getFirstMatchingAction(this.methodToVerify.name, this.methodToVerify.matchers);
const secondMethodAction = method.mocker.getFirstMatchingAction(method.name, method.matchers);
let mainMethodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
let secondMethodAsString = this.methodCallToStringConverter.convert(method);
let errorBeginning = 'Expected "' + mainMethodToVerifyAsString + 'to be called after ' + secondMethodAsString;
Expand Down
4 changes: 2 additions & 2 deletions src/MethodToStub.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {MethodStubCollection} from './MethodStubCollection';
import {Matcher} from './matcher/type/Matcher';
import {Mock} from './Mock';
import {Mocker} from './Mock';

export class MethodToStub {
constructor(public methodStubCollection: MethodStubCollection,
public matchers: Array<Matcher>,
public mock: Mock,
public mocker: Mocker,
public name: string) {
}
}
7 changes: 6 additions & 1 deletion src/Mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {MethodStub} from "./stub/MethodStub";
import {RedundantMethodNameInCodeFinder} from "./utils/RedundantMethodNameInCodeFinder";
import {strictEqual} from "./ts-mockito";

export class Mock {
export class Mocker {
private methodStubCollections: any = {};
private methodActions: Array<MethodAction> = [];
private mock: any = {};
private instance: any = {};

constructor(private clazz: any) {
this.mock.__tsmockitoInstance = this.instance;
this.mock.__tsmockitoMocker = this;
this.createMethodStubsFromPrototypeOwnPropertyNames();
this.createMethodStubsFromPrototypeKeys();
this.createMethodStubsFromClassCode();
Expand All @@ -29,6 +30,10 @@ export class Mock {
return this.mock;
}

public reset(): void {
this.methodActions = [];
}

public getAllMatchingActions(methodName: string, matchers: Array<Matcher>): Array<MethodAction> {
let result: Array<MethodAction> = [];

Expand Down
10 changes: 7 additions & 3 deletions src/ts-mockito.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Mock} from "./Mock";
import {Mocker} from "./Mock";
import {MethodStubVerificator} from "./MethodStubVerificator";
import {MethodStubSetter} from "./MethodStubSetter";
import {AnyNumberMatcher} from "./matcher/type/AnyNumberMatcher";
Expand All @@ -11,8 +11,8 @@ import {Matcher} from "./matcher/type/Matcher";
import {StrictEqualMatcher} from "./matcher/type/StrictEqualMatcher";
export {Captor} from "./Captor";

export function mock<T>(clazz: {new(...args: any[]): T; }): T {
return new Mock(clazz).getMock();
export function mock<T>(clazz: {new(...args: any[]): T;}): T {
return new Mocker(clazz).getMock();
}

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

export function reset<T>(mock: T): void {
(mock as any).__tsmockitoMocker.reset();
}

export function anyNumber(): any {
return new AnyNumberMatcher() as any;
}
Expand Down
132 changes: 132 additions & 0 deletions test/reseting.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import {Foo} from "./utils/Foo";
import {mock, instance, verify, reset, anything} from "../src/ts-mockito";

describe("resetting mocked object", () => {
let mockedFoo: Foo;
let foo: Foo;

beforeEach(() => {
mockedFoo = mock(Foo);
foo = instance(mockedFoo);
});

describe("when method has been called once", () => {
describe("but later stub has been reset", () => {
it("shows that never been called", () => {
// given
foo.getBar();
verify(mockedFoo.getBar()).once();

// when
reset(mockedFoo);

// then
verify(mockedFoo.getBar()).never();
});
});
});

describe("when method has been called thrice", () => {
describe("but later stub has been reset", () => {
it("shows that never been called", () => {
// given
foo.getBar();
foo.getBar();
foo.getBar();
verify(mockedFoo.getBar()).thrice();

// when
reset(mockedFoo);

// then
verify(mockedFoo.getBar()).never();
});
});
});

describe("when method has been called with arguments twice", () => {
describe("but later stub has been reset", () => {
it("shows that never been called", () => {
// given
foo.sumTwoNumbers(2, 3);
foo.sumTwoNumbers(2, 3);
verify(mockedFoo.sumTwoNumbers(2, 3)).twice();

// when
reset(mockedFoo);

// then
verify(mockedFoo.sumTwoNumbers(2, 3)).never();
verify(mockedFoo.sumTwoNumbers(anything(), anything())).never();
});
});
});

describe("when two different methods has been called twice", () => {
describe("but later stub has been reset", () => {
it("shows that never been called", () => {
// given
foo.getBar();
foo.getBar();
foo.sumTwoNumbers(2, 3);
foo.sumTwoNumbers(2, 3);
verify(mockedFoo.getBar()).twice();
verify(mockedFoo.sumTwoNumbers(2, 3)).twice();

// when
reset(mockedFoo);

// then
verify(mockedFoo.getBar()).never();
verify(mockedFoo.sumTwoNumbers(2, 3)).never();
verify(mockedFoo.sumTwoNumbers(anything(), anything())).never();
});
});
});

describe("when two different methods has been called", () => {
describe("but later stub has been reset", () => {
it("throws exception with information that methods has not been called", () => {
// given
foo.getBar();
foo.sumTwoNumbers(2, 3);
verify(mockedFoo.getBar()).calledBefore(mockedFoo.sumTwoNumbers(2, 3));

// when
reset(mockedFoo);

// then
try {
verify(mockedFoo.getBar()).calledBefore(mockedFoo.sumTwoNumbers(2, 3));
fail();
} catch (e) {
expect(e.message).toContain('to be called before');
expect(e.message).toContain('but none of them has been called');
}
});
});
});

describe("when two different methods has been after", () => {
describe("but later stub has been reset", () => {
it("throws exception with information that methods has not been called", () => {
// given
foo.getBar();
foo.sumTwoNumbers(2, 3);
verify(mockedFoo.sumTwoNumbers(2, 3)).calledAfter(mockedFoo.getBar());

// when
reset(mockedFoo);

// then
try {
verify(mockedFoo.sumTwoNumbers(2, 3)).calledAfter(mockedFoo.getBar());
fail();
} catch (e) {
expect(e.message).toContain('to be called after');
expect(e.message).toContain('but none of them has been called');
}
});
});
});
});

0 comments on commit 854041c

Please sign in to comment.