@@ -8,7 +8,8 @@ A simple little class and a helper function that help make Observable testing a
88[ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-green.svg )] ( https://opensource.org/licenses/MIT )
99[ ![ codecov] ( https://img.shields.io/codecov/c/github/hirezio/observer-spy.svg )] ( https://codecov.io/gh/hirezio/observer-spy ) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
1010[ ![ All Contributors] ( https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square )] ( #contributors- )
11- <!-- ALL-CONTRIBUTORS-BADGE:END -->
11+
12+ <!-- ALL-CONTRIBUTORS-BADGE:END -->
1213
1314<div align =" center " >
1415 <a href =" http://testangular.com/?utm_source=github&utm_medium=link&utm_campaign=observer-spy " >
@@ -135,6 +136,32 @@ it('should spy on Observable errors', () => {
135136});
136137```
137138
139+ ## Quick Usage with ` subscribeAndSpyOn(observable) `
140+
141+ You can also create an ` ObserverSpy ` and immediately subscribe to an observable with this simple helper function.
142+ Observer spies generated that way will provide an additional ` unsubscribe() ` method that you might want to call
143+ if your source observable does not complete or does not get terminated by an error while testing.
144+
145+ ** Example:**
146+
147+ ``` js
148+ import { subscribeAndSpyOn } from ' @hirez_io/observer-spy' ;
149+
150+ it (' should immediately subscribe and spy on Observable ' , () => {
151+ const fakeObservable = of (' first' , ' second' , ' third' );
152+
153+ // get an "ObserverSpyWithSubscription"
154+ const observerSpy = subscribeAndSpyOn (fakeObservable);
155+ // and optionally unsubscribe
156+ observerSpy .unsubscribe ();
157+
158+ expect (observerSpy .getFirstValue ()).toEqual (' first' );
159+
160+ // or use the shorthand version:
161+ expect (subscribeAndSpyOn (fakeObservable).getFirstValue ()).toEqual (' first' );
162+ });
163+ ```
164+
138165# Testing Async Observables
139166
140167#### ` it('should do something', fakeTime((flush) => { ... flush(); }); `
@@ -314,6 +341,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
314341
315342<!-- markdownlint-enable -->
316343<!-- prettier-ignore-end -->
344+
317345<!-- ALL-CONTRIBUTORS-LIST:END -->
318346
319347This project follows the [ all-contributors] ( https://github.com/all-contributors/all-contributors ) specification. Contributions of any kind welcome!
0 commit comments