Unit testing fails with the $ digest iteration loop after upgrading to 1.3.4

I have the following in my controller: I'm testing

angular.module('dashboard')
  .controller('LoginCtrl', ['$scope', 'UserService', '$rootScope', '$state', 'PageTitle', '$translate',
    function ($scope, UserService, $rootScope, $state, PageTitle, $translate) {
      $translate('TITLE_LOGIN').then(function (text) {
        PageTitle.setTitle(text);
      });
      $scope.credentials = {};
      $scope.error = false;
      $scope.loggingIn = false;
      $scope.login = function () {
        $scope.loggingIn = true;
        UserService.login($scope.credentials)
          .then(function (result) {
            if (result) {
              $scope.error = false;
              $state.go('main.start');
            } else {
              $scope.loggingIn = false;
              $scope.error = true;
            }
          }, function () {
            $scope.loggingIn = false;
            $scope.error = true;
          });
      };
    }]);

where the service calls some server with $ http.

Below is my test for login function

describe('Controller: LoginCtrl', function () {

  // load the controller module
  beforeEach(module('dashboard'));

  var LoginCtrl,
    scope,
    userSvc,
    conf,
    base64,
    localStorage,
    httpBackend;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope, $httpBackend, UserService, configuration, Base64, localStorageService) {
    scope = $rootScope.$new();
    LoginCtrl = $controller('LoginCtrl', {
      $scope: scope
    });
    httpBackend = $httpBackend;
    userSvc = UserService;
    conf = configuration;
    base64 = Base64;
    localStorage = localStorageService;
    sessionStorage.clear();
  }));

  it('should login', function () {
    var token = base64.encode('1:1');
    httpBackend.expectGET('scripts/i18n/nl.json').respond(200);
    httpBackend.expectGET('views/login.html').respond(200);
    httpBackend.whenPOST(conf.backend + 'users/login').respond({'access_token': 1});
    httpBackend.expectGET('views/main.html').respond(200);
    httpBackend.expectGET('views/start.html').respond(200);
    scope.login({'email': 'tester'});
    httpBackend.flush();
    expect(sessionStorage.accessToken).toEqual(token);
    expect(userSvc.profile()).toBeObject();
  });
});

This test passes through to version 1.3.3, but in 1.3.4 I get the following error when httpBackend.flush ()

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
    Watchers fired in the last 5 iterations: []
    http://errors.angularjs.org/1.3.4/$rootScope/infdig?p0=10&p1=%5B%5D
        at /srv/http/bower_components/angular/angular.js:14170
        at /srv/http/bower_components/angular-mocks/angular-mocks.js:1523
        at /srv/http/test/unit/controllers/login.js:38

From the change log, I could not figure out which of the changes would affect it. Any ideas?

+4
source share
1 answer

For me it has something to do with $location.url(). When I mocked $locationin my test, the error no longer occurred.

var mockLocation = {
    url: function(url) {
        // ignore...
    }
};

beforeEach(function () {

    module(function ($provide) {
        $provide.value('$location', mockLocation);
    });

    // ....
})
+4
source

All Articles