Is the MockBackend way of testing HTTP calls deprecated in Angular4?
Asked Answered
H

0

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.

Harl answered 24/11, 2017 at 13:5 Comment(4)
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 angular.io/guide/http.Schwinn
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: 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.Schwinn
@jonrsharpe:- Will this do ? medium.com/spektrakel-blog/…Harl
Let us continue this discussion in chat.Harl

© 2022 - 2024 — McMap. All rights reserved.