(better) support for RXJS subscriptions
See original GitHub issueSo 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:
- Created 3 years ago
- Comments:10 (8 by maintainers)
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!
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.