How to unit test the angularjs injector, outside of a controller?

46 Views Asked by At

As part of an AngualrJS app, I have to retrieve user details from a REST api before bootstrapping the application.

var initInjector = angular.injector(['ng']);
var $http = initInjector.get('$http');

$http.get('../api/user').then(
  function (response) {
    if (response.user.valid) {
      angular.bootstrap(document, ['app']);
    }
  },
  function () {
    window.location.href = '../userError.html';
  }
});

I'm building the application using gulp, and have been trying to unit test this as follows

describe('unit test app config', function () {
  beforeEach(function () {
    module('app');
    inject();
  });

  it('should log a message on calling the injector', function () {
    spyOn(angular, 'injector').and.callThrough();
    expect(angular.injector).toHaveBeenCalled();
  });
});

but this is giving the following error: Expected spy injector to have been called.

How can I unit test this code? I just need to mock the $http service and test the success and failure functions

0

There are 0 best solutions below