定义单元测试中的当前状态

时间:2018-03-22 14:44:34

标签: angularjs unit-testing

我已移动此报告'功能从单个模块(称为“aam'”)进入核心,以便其他模块(例如' bbc')可以使用它。

现在我正在重写单元测试。

我得到的咕噜声错误

should go state aam.reports with URL_NOT_SPECIFIED
     reports-state spec
     TypeError: 'null' is not an object 
     (evaluating 'BbpcConfiguration.getProperty(configProperty).then')

向我表明$ state为空或结构不正确。

以下是报告控制器:

(function() {
    'use strict';
    angular.module('com.ct.bbpcCore')
    .controller('reportController', ['$window', '$state', 'BbpcUserService', 'BbpcConfiguration', function ($window, $state, BbpcUserService,BbpcConfiguration) {
        angular.element(document).ready(function () {
            //Get url base on locale
            var reportUrl = "URL_NOT_SPECIFIED";

            var currentState = $state.current.name;
            var configProperty = "";
            var title = "";

            if (currentState.indexOf('aam.reports')) {
                configProperty = 'report.aam.link';
                title = "AAM.REPORT";
            };
            if (currentState.indexOf('bbc.reports')) {
                configProperty = 'report.bbc.link';
                title = "BBC.REPORT";
            };

            BbpcConfiguration.getProperty(configProperty).then(function(response) {
                if (response) {
                    var language = BbpcUserService.getLanguageCd() || "en_CA";
                    reportUrl = response[language] || reportUrl;
                }
                var spec = "width=" + $window.outerWidth + ", height=" + $window.outerHeight;
                $window.open(reportUrl, title, spec);
            });
        });
    }]);
}());

这是report-controller.spec:

describe('reports-state spec', function() {

  'use strict';

  var $injector, $window, $rootScope,
      $state,  BbpcConfiguration, reportController, $controller, BbpcUserService;

  beforeEach(function() {

    module('com.ct.bbpcCore', function($provide) {
      $provide.value('BbpcConfiguration', BbpcConfiguration = {
        getProperty: function(key){
          if('report.aam.link' === key){
            return {
              "fr_CA": "https://eng-link",
              "en_CA": "https://fre-link"
            };
          }
          return null;
        }

      });
    });
    inject(function(_$injector_) {
      $injector = _$injector_;
      $window = $injector.get('$window');
      $state = $injector.get('$state');
      $rootScope = $injector.get('$rootScope');
      $controller =$injector.get('$controller');
      BbpcUserService =$injector.get('BbpcUserService');

    });
  });

   it('should go state aam.reports with URL_NOT_SPECIFIED', function() {
     $state.current = {'name': 'aam.reports' };

     spyOn($window, 'open').andCallFake(function(){});
     reportController = $controller('reportController', {'$window':$window, '$state':$state, 'BbpcUserService':BbpcUserService, 'reportLink':undefined});
     $state.go('aam.reports');
    $rootScope.$apply();
    expect($state.current.name).toEqual('aam.reports');
    expect($window.open).toHaveBeenCalledWith('URL_NOT_SPECIFIED', 'AAM.REPORT', 'width=0, height=0');
  });
});

我尝试添加 $ state.current = {' name':' aam.reports'在'它'中阻止,但这不是它正在寻找的东西。

不确定如何调试单元测试。 :P我不能使用console.log($ state)来查看它。

0 个答案:

没有答案