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.
@angular/http
module is deprecated, in favour of@angular/common/http
which has its ownHttpClientTestingModule
. It actually says that in the docs: "use @angular/common/http instead", which is covered in angular.io/guide/http. – SchwinnHttpClient
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 neitherMockBackend
nor usingspyOn
, but actually using the testing facilities the framework provides. – Schwinn