AngularJs中控制器的单元测试失败

时间:2016-07-29 23:31:50

标签: angularjs unit-testing

这是我的控制器:

'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');
      });
    };
  }]);

以下是我对该控制器的单元测试,以确保函数goTo是函数:

'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');
  });
});

单元测试失败了,我不知道这个单元测试有什么问题。 有什么建议在这里是什么问题。 注意:错误消息为:范围未定义。

提前致谢 -k

0 个答案:

没有答案