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

Fnmock #6

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ Syntax is the same as with getter values.

Please note, that stubbing properties that don't have getters only works if [Proxy](http://www.ecma-international.org/ecma-262/6.0/#sec-proxy-objects) object is available (ES6).

### Mocking free functions

Sometimes you need to mock a function, not an object, for example to pass as a callback somewhere. This can be done using `fnmock()`. It works just like any other mock, except it's a function, not an object.

```typescript
let fn: (a: number, b: string) => number = fnmock();
when(fn(10, 'hello')).thenReturn(5);

instance(fn)(10, 'hello'); // returns 5
verify(fn(10, 'hello')).called();
```

### Call count verification

``` typescript
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"ts-helpers": "^1.1.2",
"ts-jest": "^23.0.1",
"tslint": "^5.7.0",
"typescript": "^2.7.2"
"typescript": "^3.4.5"
},
"dependencies": {
"lodash": "^4.17.5"
Expand Down
2 changes: 1 addition & 1 deletion src/Mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class Mocker {
}

private createMethodToStub(key: string): () => any {
return (...args) => {
return (...args: any[]) => {
if (!this.methodStubCollections[key]) {
this.methodStubCollections[key] = new MethodStubCollection();
}
Expand Down
12 changes: 12 additions & 0 deletions src/ts-mockito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ export function mock<T>(clazz: (new(...args: any[]) => T) | (Function & { protot
return new Mocker(clazz).getMock();
}

export function fnmock<R, T extends any[]>(): (...args: T) => R {
class Mock {
public fn(...args: T): R { return null as R; }
}

const m: Mock = mock(Mock);
(m.fn as any).__tsmockitoInstance = instance(m).fn;
(m.fn as any).__tsmockitoMocker = (m as any).__tsmockitoMocker;
return m.fn;
}

export function verify<T>(method: T): MethodStubVerificator<T> {
return new MethodStubVerificator(method as any);
}
Expand Down Expand Up @@ -127,6 +138,7 @@ export function objectContaining(expectedValue: Object): any {
export default {
spy,
mock,
fnmock,
verify,
when,
instance,
Expand Down
51 changes: 51 additions & 0 deletions test/mocking.functions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { capture, fnmock, instance, reset, resetCalls, verify, when } from "../src/ts-mockito";

describe("mocking", () => {
describe("mocking functions", () => {
it("should mock free functions", () => {
const fn: () => number = fnmock();

when(fn()).thenReturn(1);

expect(instance(fn)()).toEqual(1);
verify(fn()).called();
});

it("should match arguments of free functions", () => {
const fn: (a: string, b: number) => number = fnmock();

when(fn("a", 1)).thenReturn(1);

expect(instance(fn)("a", 1)).toEqual(1);
expect(instance(fn)("a", 2)).toBeNull();
verify(fn("a", 1)).called();
});

it("should reset mocks", () => {
const fn: () => number = fnmock();

when(fn()).thenReturn(1);
expect(instance(fn)()).toEqual(1);

reset(fn);
expect(instance(fn)()).toBeNull();
});

it("should reset calls", () => {
const fn: () => number = fnmock();

instance(fn)();
verify(fn()).once();

resetCalls(fn);
verify(fn()).never();
});

it("should capture parameters", () => {
const fn: (a: string) => void = fnmock();

instance(fn)("a");
expect(capture(fn).last()).toEqual(["a"]);
});
});
});