-
-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(better) support for RXJS subscriptions #226
Comments
Hi @Martinspire, thanks for the report. Might you provide an example of the code you want to test? Usually you might use Unfortunately, return types exist only in typescript code. Therefore in javascript runtime we don't have them, and I don't know a solution to detect them. That's why angular uses decorators, it allows to collect information about constructors for DI. It's quite hard, if it is even possible, to predict what should be returned and how it should behave, and looks like, only developers know the right answer. What I usually do is: MockBuilder(Component, Module).mock(Service, {prop$: EMPTY}); or in case of more complicated cases MockInstance(Service, (service, injector) => {
service.prop$ = of(injector.get(TOKEN));
}); Without ngMocks, code would be almost the same, but with more lines of code. TestBed.configureTestingModule({
providers: [{
provide: Service,
useValue: {
prop$: EMPTY
},
}],
}) So, still, if we want to mock real code - we have to spend some time to provide a proper stub object. |
From your suggestions, I can add a section in readme how to mock observables in constructors and lifecycle hooks. |
Hi @Martinspire, might you check whether the section fits your expectations? https://github.com/ike18t/ng-mocks#how-to-fix-typeerror-cannot-read-property-subscribe-of-undefined |
A test example is here: https://codesandbox.io/s/happy-ellis-6c47w?file=/src/test.spec.ts:1412-1418 |
## [10.5.3](v10.5.2...v10.5.3) (2020-11-07) ### Bug Fixes * an example how to handle "TypeError: Cannot read property 'subscribe' of undefined" ([6501a87](6501a87)), closes [#226](#226) * info how to solve "type is part of the declarations of 2 modules" ([f5ee1bc](f5ee1bc)) * mock-render proxy ([eaeabba](eaeabba)) * relaxed signature of MockInstance ([dccaa2d](dccaa2d))
v10.5.3 has been released and contains a fix for the issue. |
Hi @Martinspire, I hope this finds you well. I think I found a way to implement what you described: https://github.com/ike18t/ng-mocks#ngmocksdefaultmock Might you check if this fits your expectations? ng-mocks.zip Here you can find examples for tests: https://github.com/ike18t/ng-mocks/tree/master/tests/ng-mocks-default-mock Looking forward to hearing from you. |
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. |
Sorry for the lack of activity. Just checked a few links and they were not found but I just looked at the 2 releases and it fixes my issue very nicely. Thanks for the help and explanation! |
Hi @Martinspire, glad to see you back :) Might I ask you to provide a feedback or to drop a few words how Thank you in advance! |
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! |
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 testingThe text was updated successfully, but these errors were encountered: