I want to write a functional test for my app, and I want to mock my loginService service, to speed up the tests. I do not want to mock the $http, but the service that internally calls it, but I can't put a spy on the function loginService.validate, which sends a HTTP query to the server, but it is not loaded, since it is in the driver, not in the test itself. I also took a look at addMockModule but I can't understand how to use it. Is there a simple way to do what I want? I looked at these, but they did not help:
Asked
Active
Viewed 9,509 times
9
-
Have you looked into: https://github.com/kbaltrinic/http-backend-proxy/wiki – reutsey Oct 03 '16 at 14:57
1 Answers
5
Try this.
describe('MockingHttp', function() {
beforeEach(function() {
browser.addMockModule('httpMocker', function() {
angular.module('httpMocker', ['ngMockE2E'])
.run(function($httpBackend) {
$httpBackend.whenGET(
'http://jsonplaceholder.typicode.com/photos')
.respond([
{
albumId: 1,
id: 1,
title: "accusamus beatae ad",
url: "http://placehold.it/600/92c952",
thumbnailUrl: "http://placekitten.com/g/200/300"
}
])
})
})
});
Read this for more clarification. :)

Manuli
- 1,203
- 4
- 20
- 43
-
Another useful example (which doesn't use `$httpBackend`, nor `ngMockE2E`, but simply wants to "replace" a service with behavior needed for the test) [shown here](https://github.com/angular/protractor/issues/667) – Nate Anderson Aug 02 '17 at 05:17