6

I am trying to test the HTTP calls and referring to the following page to write unit tests: https://angular-2-training-book.rangle.io/handout/testing/services/mockbackend.html

But when I looked on the Angular website it looks like the MockBackend class is deprecated: https://angular.io/api/http/testing/MockBackend

The same website has a different way to test HTTP calls: https://angular-2-training-book.rangle.io/handout/testing/services/alt-http-mocking.html

I also came across an interesting article below (as I am trying to test login so making a POST call): http://jasonwatmore.com/post/2016/11/24/angular-2-mockbackend-example-for-backendless-development

So I wanted to know which is the best or latest way to test HTTP calls? I am a bit confused to whether use MockBackend or use the spyOn mocking strategy.

Abhishek Sharma
  • 1,420
  • 1
  • 20
  • 30
  • 7
    It's deprecated because the whole `@angular/http` module is deprecated, in favour of `@angular/common/http` which has its own `HttpClientTestingModule`. It actually **says that in the docs**: *"use @angular/common/http instead"*, which is covered in https://angular.io/guide/http. – jonrsharpe Nov 24 '17 at 13:06
  • 2
    As I thought was obvious, I (and the Angular team) suggest you use the `HttpClient` that *isn't* deprecated, following the above guidance in the official Angular documentation. If you have an existing site, I actually wrote a bit about converting: https://blog.jonrshar.pe/2017/Sep/11/http-client-conversion.html. Note that this is *neither* `MockBackend` nor using `spyOn`, but actually using the testing facilities the framework provides. – jonrsharpe Nov 24 '17 at 13:08
  • @jonrsharpe:- Will this do ? https://medium.com/spektrakel-blog/angular-testing-snippets-httpclient-d1dc2f035eb8 – Abhishek Sharma Nov 24 '17 at 13:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/159757/discussion-between-mihawk-and-jonrsharpe). – Abhishek Sharma Nov 24 '17 at 13:15

0 Answers0