1

Most Angular tutorials I have seen inject the HTTP service into other more specialized services. I'm planning to create a specialized service and inheriting from the HTTP service.

However, I'm concerned about testability because I think that, if I inject it, my specialized service will be easier to test.

My question is - is it advisable to inherit the HTTP service from a general point of view and for testability purposes?

user11081980
  • 3,059
  • 4
  • 30
  • 48
  • Possible duplicate of [Difference between Encapsulation and Abstraction](https://stackoverflow.com/questions/15176356/difference-between-encapsulation-and-abstraction) – Reactgular Jan 22 '18 at 17:33
  • It's not clear what you're trying to achieve, or which version of Angular. Anything above 4.3, you can probably do what might make you consider inheriting the HTTP client with interceptors in the new `HttpClient` API. – jonrsharpe Jan 22 '18 at 17:33
  • This is an opinion question. The benefits of either approach have been widely discussed as this is a general OOP issue. – Reactgular Jan 22 '18 at 17:34

1 Answers1

1

I'd recommend sticking with injecting it. Angular httpclient now (4.3 and above) has a dedicated testing module: https://angular.io/api/common/http/testing/HttpClientTestingModule

This allows you to inject it in your test suite and use it to both monitor requests and then mock responses as appropriate.

There's a good walk-through of using it here: https://alligator.io/angular/testing-httpclient/

match
  • 10,388
  • 3
  • 23
  • 41