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

Added new examples to README #230

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Mocking library for TypeScript inspired by http://mockito.org/

## Main features


* Strongly typed
* IDE autocomplete
* Mock creation (`mock`) (also abstract classes) [#example](#basics)
Expand All @@ -26,6 +25,7 @@ Mocking library for TypeScript inspired by http://mockito.org/
* Capturing arguments passed to method (`capture`) [#example](#capturing-method-arguments)
* Recording multiple behaviors [#example](#recording-multiple-behaviors)
* Readable error messages (ex. `'Expected "convertNumberToString(strictEqual(3))" to be called 2 time(s). But has been called 1 time(s).'`)
* Mocking within other functions [#example](#mocking-within-other-functions)

## Installation

Expand Down Expand Up @@ -85,6 +85,22 @@ let foo:Foo = instance(mockedFoo);
console.log(foo.sampleGetter);
```

in th case that your getter returns an object, make sure that you return the expected object rather than trying to access its properties from within `when`:

``` typescript
// Creating mock
let mockedFoo:Foo = mock(Foo);

// stub getter before execution
when(mockedFoo.sampleGetter).thenReturn({ name: 'three' });

// Getting instance
let foo:Foo = instance(mockedFoo);

// prints three
console.log(foo.sampleGetter.name);
```

### Stubbing property values that have no getters

Syntax is the same as with getter values.
Expand Down Expand Up @@ -365,6 +381,24 @@ foo.bar();
console.log(capture(spiedFoo.bar).last()); // [42]
```

### Mocking within other functions

To mock class objects within other functions, it works exactly the same as mocking outside of functions

``` typescript
const baz = () => {
const foo = new Foo();
return foo.getBar(1);
}

const mockedFoo = mock(Foo);
when(mockedFoo.getBar(1)).thenReturn(5);

console.log(baz()); // 5

verify(mockedFoo.getBar(1)).once();
```

### Thanks

* Szczepan Faber (https://www.linkedin.com/in/szczepiq)
Expand Down