Skip to content

Commit

Permalink
docs(createfunctionspy): add documentation for createFunctionSpy
Browse files Browse the repository at this point in the history
  • Loading branch information
shairez committed Sep 5, 2020
1 parent 617b414 commit 9fad81f
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,51 @@ MyClass{
})

```

### 9. Spying on a function

You can create an "auto spy" for a function using:

```ts
import { createFunctionSpy } from 'jasmine-auto-spies';

describe('Testing a function', () => {
it('should be able to spy on a function', () => {
function addTwoNumbers(a, b) {
return a + b;
}

const functionSpy = createFunctionSpy<typeof addTwoNumbers>('addTwoNumbers');

functionSpy.and.returnValue(4);

expect(functionSpy()).toBe(4);
});
});
```

This is useful if you have an observable returning function and you want to use `nextWith` for example:

```ts
import { createFunctionSpy } from 'jasmine-auto-spies';
import { Observable, of } from 'rxjs';
import { ObserverSpy } from '@hirez_io/observer-spy';

describe('Testing an observable function', () => {
it('should be able to spy on observable', () => {
function getResultsObservable(): Observable<number> {
return of(1, 2, 3);
}

const functionSpy = createFunctionSpy<typeof getResultsObservable>(
'getResultsObservable'
);

functionSpy.and.nextWith(4);
const observerSpy = new ObserverSpy();
functionSpy.subscribe(observerSpy);

expect(observerSpy.getLastValue()).toBe(4);
});
});
```

0 comments on commit 9fad81f

Please sign in to comment.