vendredi 24 juillet 2015

Issue with closing login module for jasmine testing

I have an application which uses angulars $modal to popup a login modal any time a user tried to enter a secure route without a valid authentication token. This works great but is causing an issue with my testing.

The modal was created as a factory

  .factory('loginModal', function ($modal) {
      return function() {
          var instance = $modal.open({
            templateUrl: 'partials/login',
            controller: 'AuthCtrl',
            controllerAs: 'AuthCtrl'
          })

        return instance.result;
      };
    });

In my controller I have a login action, upon successfully logging in the modal is closed using $scope.$close.

$scope.login = function() {
      auth.login($scope.user)
        .then(function(response) {
          $scope.$close(response);
          $state.go('secure.user');
        }, function(response) {
          $scope.hasErrMsg = true;
          $scope.errMsg = 'Incorrect password.';
          $scope.$dismiss;
        });
    };

Lastly my unit test which is checking to make sure that auth.login is called with the correct properties when my controllers login function is called.

describe('AuthCtrl.login()', function () {
    it('should call auth.login() with $scope.user', function () {
        $scope.user = {
            email: 'bwayne@wayneenterprise.com',
            password: 'password123'
        };
        spyOn(auth, 'login').and.returnValue(deferred.promise);
        $scope.login();
        deferred.resolve();
        $scope.$digest();
        expect(auth.login).toHaveBeenCalledWith($scope.user);
    });
});

Now when I run the test I get the following error:

TypeError: $scope.$close is not a function

I suspect this error is because in code expects $scope to be set to the scope for the modal when it calls $scope.$close and in my test $scope is set to the controllers scope. Although I'm not sure how to reference the $modal's scope.

Aucun commentaire:

Enregistrer un commentaire