I am writing a test for a component where i use mock for a service. But this service has a property which is reference to another service and utilizes some methods inside the service. How can i create proper mock for this.
Asked
Active
Viewed 41 times
0
-
this service doesn't use any reference to any other service anymore since it's mocked. So all its methods do nothing except what you tell them to do. – JB Nizet Nov 07 '18 at 12:31
-
the component calls service1.service2.methodInService2() , . this fails when i mock the service1. I got one solution, i added the second service as a property inside the first service mock and worked. – Kiran Krishnan Nov 07 '18 at 13:40
-
You mean you have a public field in service1? That's awful. Don't use public fields. Use methods. The service1 should delegate to the service2, not expose it. If it exposes it, at the very least, expose it using a getService2() method that you can mock to make it return a fake service2. – JB Nizet Nov 07 '18 at 14:07
-
Seems the idea is nice. By using a getter method for service, can help to mock the behaviour of the second service itself . Thanks – Kiran Krishnan Apr 08 '19 at 10:46