Saturday, September 7, 2019

When testing a service that does not implement a contract in an Angular app you still replace and fake the service.

In the providers for the test have something like so:

{provide: MyService, useClass: MyFakeService}

 
 

MyFakeService in this example can just be an any type object. At the constructor of the component wired up by TestBed the external dependency of the service will be wired up with MyFakeService in spite of any specific type for the service denoted right there at the constructor inline and when methods are called on the service by the test they had better have dance partners at MyFakeService. However, MyFakeService does not need to implement MyService for this trick to work or anything like that. For further reading:

No comments:

Post a Comment