Mocking rest calls in functional e2e protractor tests in angular 4 project

Aniruddha Das picture Aniruddha Das · Sep 8, 2017 · Viewed 8.6k times · Source

We have a huge project and we have written a lot of test cases to cover a lot of real scenario user behavior in our e2e functional test cases.

As the test goes on, it makes a lot of rest calls to complete the test cases. When we cover different browser in sauce lab it multiplies 5-9 times.

What I want is to mock all the rest calls so that there will not be any real calls to the actual server but internally it will be handled. its for functional e2e test not unit test as we mocked all unit tests using jasmine spyOn.

I have explored json-server and $httpBackend of protractor. son-server did not fit as it does not handles app post, put, delete calls well. in case of $httpBackend

I have gone through this post and its for andularJs app not angular app and also its all about single rest call mocking for angularJs not for all rest calls.

Also looked this angularjs multi mock for angularjs and its also for angularjs not angular and looks like this changes the original rest url for query parameter.

Answer

glucas picture glucas · Oct 16, 2017

Mocking network calls is not supported yet by Protractor for Angular 2+ applications. See this post and the corresponding issue on github.

Personnally, I used ng-apimock plugin to mock all network calls to the backend with a proxy.

It works really well and is simple to configure. Just follow the configuration for Angular application here