Mock a service in E2E test in protractor

user5134233 picture user5134233 · May 14, 2016 · Viewed 8.6k times · Source

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:

Mocking and Stubbing with protractor

Mocking an angular service in Protractor

Answer

Manuli picture Manuli · May 16, 2016

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. :)