i see lots of examples about how to unit test simple components in angular 2+, but when it comes to test components who use services, it becomes a nightmare to maintain the test bed providers and imports. how can i avoid it ?
for example i have myComponents, who uses myService, who uses HttpClient. To test myComponent i must setup the providers for myService and HttpClient. If i add an other service to the constructor of myService, i will have to edit ALL the testbeds of the cmoponents who use this service.
can't i tell the testbed to fetch the default dependencies for these modules?
The problem results from wrong testing methodology. Unit testing is about testing single units.
In this scenario
myComponents, who uses myService, who uses HttpClient. To test myComponent i must setup the providers for myService and HttpClient.
it is myComponent unit that it tested. This means that any other unit should be mocked or stubbed, including myService.
While this
fetch the default dependencies for these modules
is considered not unit but integration/e2e test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With