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.

(better) support for RXJS subscriptions

See original GitHub issue

So when you mock a component or service that contains or has functions that return a subscription, it becomes difficult to easily test that with a mocked component.

Now I get that RXJS isn’t a mandatory part of Angular, but seeing how many people use it now, I feel that the types/functions should be supported to mock the response or have a way to inject a value when the mocked component is set up.

Either that or the readme should have more detail on how to easily do this, as there’s lots of folks that want to do this. Especially when the ngOnInit triggers a subscription, it requires more code to get some data in your mocked component before your first test, but you’d rather inject the data in your test so you have more control over executing multiple use cases.

I’m not entirely sure how the code should look like, but I feel that right now I can’t really use ng-mocks whenever there is a subscription in my service that the oninit of my component that I’m testing, is going to call for.

So when a service has a function that returns a subscription, I’d like ng-mocks to recognize that and initialize the subscription (with an empty value to start with, or some way to provide the initial value), so I can do something with that.

Right now you’d often get a TypeError: Cannot read property 'subscribe' of undefined because the value isn’t initialized as a subscription. The cop-out would be to just inject the service and go from there but thats not really black-box testing

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Martinspirecommented, Dec 23, 2020

I’ve only done work on a few tests lately, but it seems to have fixed my issues and improved a few other things too. I do think I won’t always use defaultMock as I still prefer to mock only what I need for my tests, but for some generic functions it seems to provide an easy interface. Thanks for the work and thanks for the feedback. Much appreciated!

1reaction
satanTimecommented, Dec 4, 2020

v11.2.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best Practices for Managing RxJS Subscriptions - This Dot Labs
One method we can use, is to unsubscribe manually from active subscriptions when we no longer require them. RxJS provides us with a...
Read more >
(better) support for RXJS subscriptions · Issue #226 - GitHub
So when a service has a function that returns a subscription, I'd like ng-mocks to recognize that and initialize the subscription (with an...
Read more >
Yet another way to handle RxJS subscriptions - Medium
The most popular is the AutoUnsubscribe. You simply subscribe and assign a Subscription instance to a class field. The decorator takes care of ......
Read more >
Subscription - RxJS
A Subscription is an object that represents a disposable resource, usually the execution of an Observable. A Subscription has one important method, unsubscribe ......
Read more >
Ditch Subscriptions, Use the Power of rxjs instead
Call an http service to fetch all players that are available to be voted for. Load details for each player such as nickname...
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