vendredi 29 juillet 2016

Unit Test for controller in AngularJs Failed

Here is my controller:

'use strict';
angular.module('pulseOneApp')
  .config(function ($stateProvider) {
    $stateProvider.state('about', {
      url: '/about',
      views: {
        'content': {
          templateUrl: 'components/version/about.html',
          controller: 'AboutController'
        }
      },
      authNotRequired: true
    });
  })

  .controller('AboutController', ['$scope', '$state', 'session', '$pulseOneProps', '$filter', 'propertiesServices', function ($scope, $state, session, $pulseOneProps, $filter, propertiesServices) {

    /**
     * @function  getServerVersion
     * @description gets the serverVersion from $pulseOneProps if exist, else makes a REST Api call using propertiesServices.
     * @returns string
     */
    var getServerVersion = function () {
      var systemProperties,serverVersion;
      if ((angular.isDefined($pulseOneProps))  && $pulseOneProps !== null) {
        systemProperties = $pulseOneProps.getProperties();
        if(systemProperties) {
          return $filter('filter')(systemProperties, {name: 'server_version'})[0].value;
        }
        else{
          //when the session exist and not able to retrieve $pulseOneProps then do REST Api call and update the systemProperties
          session.validateSession().then(function() {
            propertiesServices.getPulseOneProperties().then(function (systemProperties) {
              serverVersion=$filter('filter')(systemProperties, {name: 'server_version'})[0].value;
              // This will update the UI when serverVersion is available
              $scope.serverVersion = (serverVersion) ? serverVersion: false;
            });
          });
        }
      }
      return null; // if none of the above cases are valid then don't display the server version.
    };

    var serverVersion=getServerVersion();
    $scope.serverVersion = (serverVersion) ? serverVersion: false;

    $scope.goTo = function() {
      session.validateSession().then(function() {
        $state.go('app.dashboard');
      })
      .catch(function() {
        $state.go('login');
      });
    };
  }]);

and Here is my Unit Test for this controller to make sure the function goTo is the function:

'use strict';

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

  // load the controller's module
  beforeEach(module('ui.router'));
  beforeEach(module('ps.authentication.services'));
  beforeEach(module('ps.version'));
  beforeEach(module('pulseOneApp'));
  beforeEach(module('ps.components.properties'));


  var scope, AboutController;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($rootScope, _$controller_) {
    scope = $rootScope.$new();
    AboutController = _$controller_('AboutController', {
      $scope: scope
     });
    scope.$digest();
  }));

  it('should find to goTo function', function () {
    expect(typeof scope.goTo).toBe('function');
  });
});

The unit test is failed and I don't know what was wrong with this unit test. Any suggestion what was the issue here. Thanks in advance -k

Aucun commentaire:

Enregistrer un commentaire