DISCUSSION 3: `subscribeAndSpyOn` + `unsubscribe` VS `spyOn` and `dispose`
See original GitHub issueCONTEXT:
Following #12 (by the amazing @ThomasBurleson) we had a discussion and realized this PR could be separated into several PRs, and before that we need to discuss them.
This one is about: changing the factory method and functionality of ObserverSpyWithSubscription
.
The suggested API changes to ObserverSpy
-
export type SpyOnResults<T> = [ObserverSpy<T>, () => void, Observable<any>];
export function spyOn<T>(
source$: Observable<T>,
completionCallback?: CompletionCallback
): SpyOnResults<T> {
const spy = new ObserverSpy<T>(completionCallback);
const subscription = source$.subscribe(spy);
const dispose = () => {
subscription.unsubscribe();
removeSpyFromWatchList(spy);
};
subscriptions.push([spy, dispose, source$]);
return [spy, dispose, source$];
}
Thomas’ points:
spyOn
is shorter and cleanerdispose()
relates to the spy, not the subscription. (you shouldn’t care about what happens behind the scenes)- tuples are better because they let you name your own variables during the array destructuring.
- You separate responsibilities and leave the spy to handle spying alone.
Shai’s points:
- just the name
spyOn
hides the fact that there is a subscription happening behind the scenes, and because the subscription IS the action of the test, it’s an important information to convey.subscribeAndSpyOn
shows what actually happens behind the scenes better IMO. - Because of jasmine’s
spyOn
function, when scanning the test, people might confuse the two and not realize there’s a subscription involved here. - I always prefer to return properties over tuples, especially if I want to keep things consistent between lots of tests. It also helps to understand the API better IMO.
- I prefer the name
unsubscribe
overdispose
because it’s a term + functionality people already know.
I’d love to hear your thoughts / corrections @ThomasBurleson @katharinakoal @edbzn @burkybang @yjaaidi (and whoever is interested to share their opinion), before we decide whether to break it into its own PR.
SO… WHAT DO YOU THINK?
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
spyOn a Subscription.unsubscribe() - testing angular
const spy = spyOn( (component as any).subscription, 'unsubscribe' ).and.callThrough() component.ngOnDestroy(); expect(spy).
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey Guys, great work! Here are my points 😃
I prefer the
subscribeAndSpyOn
overnew ObserverSpy()
call. I think its functionality is what most users needs - subscribe and spy. I think the name should be shorter, something likespyOnObserver
orsubscribeSpyTo
.I prefer to return an object rather than an array for two reasons: The order doesn’t matter and name consistency.
I like the additional functionality of automatically unsubscribe inside the
afterEach
call.I think that in this case,
unsubscribe
is more accurate thandispose
.Last but not least, the
README
should indicate that this library is meant for advanced use cases. If users need to write simple specs, they could create the following function:Great work @shairez @katharinakoal @ThomasBurleson
Sounds good to me, too! 👌