question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DISCUSSION 3: `subscribeAndSpyOn` + `unsubscribe` VS `spyOn` and `dispose`

See original GitHub issue

CONTEXT:

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 cleaner
  • dispose() 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 over dispose 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:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
NetanelBasalcommented, Jul 5, 2020

Hey Guys, great work! Here are my points 😃

  • I prefer the subscribeAndSpyOn over new ObserverSpy() call. I think its functionality is what most users needs - subscribe and spy. I think the name should be shorter, something like spyOnObserver or subscribeSpyTo.

  • 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 than dispose.

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:

function spyOnObserver<T>(source: Observable<T>, baseName = '') {
  const spy = jasmine.createSpyObj<Observer<T>>(baseName, [
    'next',
    'error',
    'complete',
  ]);

  return {
    unsubscribe: source.subscribe(spy),
    spy,
  };
}

Great work @shairez @katharinakoal @ThomasBurleson

1reaction
katharinakoalcommented, Jul 20, 2020

Sounds good to me, too! 👌

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found