AngularJS单元测试-注入并创建了Window对象,但其属性未定义

时间:2019-01-31 18:03:37

标签: angularjs unit-testing jasmine karma-jasmine

我正在尝试进行单元测试,以检查控制器是否已正确初始化。我一直遇到错误“ TypeError:无法读取未定义的属性'isImageBuilderSession'”。

isImageBuilderSession是在WindowObject接口中定义的变量,用于扩展角度窗口服务。传递给我的控制器的$ window变量定义为WindowObject。我可以注销它,并看到它正在按预期方式注入/创建。不确定为什么其值未定义。

WINDOW对象界面:

  export interface IPhotonWindowObject extends angular.IWindowService {
    siteConfig: IPhotonSiteConfig;
    d3: any;
    cccNativeClient?: Interfaces.NativeClient;
    CefSharp?: any;
  }

控制器:

/// <reference path="../_references.d.ts"/>
"use strict";
module Controllers {

  import IPromise = angular.IPromise;
  import IPhotonCatalogsResponse = Interfaces.IPhotonCatalogsResponse;
  import IPhotonHttpError = Interfaces.IPhotonHttpError;
  import IScope = angular.IScope;
  import IPhotonWindowObject = Interfaces.IPhotonWindowObject;
  import IPhotonGetUserSessionResponse = Interfaces.IPhotonGetUserSessionResponse;

  interface ICatalogControllerScope extends IScope {}

  export class CatalogController {

    public static controllerName = "CatalogController";

    public static $inject = [
      "$scope",
      "$location",
      "$window",
      Constants.Photon.SERVICE_LOGGER,
      Constants.Photon.SERVICE_CATALOG,
      Constants.Photon.SERVICE_FEEDBACK
    ];

    public catalogs: Array<Interfaces.IPhotonCatalog>;
    private TAG = "CatalogController";

    constructor(protected $scope: ICatalogControllerScope,
                protected $location: angular.ILocationService,
                protected $window: IPhotonWindowObject,
                protected loggerService: Services.LoggerService,
                protected catalogService: Services.CatalogService,
                protected feedbackService: Services.FeedbackService) {

      if (this.$window.siteConfig.isImageBuilderSession) {
        let reference = "image-builder/" + this.$window.siteConfig.stack; // Image builder itself is considered both stack & fleet.
        this.gotoReserveInstancePage(reference);
        return;
      }

      this.catalogService.getCatalog().then((data: Interfaces.IPhotonCatalogsResponse) => {
        this.catalogs = data.catalogs;
      }).catch((error) => {
        this.loggerService.error("CatalogController", error);
      });

      $scope.$on("launchApp", this.launchApp);
    }

    public gotoReserveInstancePage(reference: string, appId?: string) {
      if(appId){
        this.$location.path("/reserve").search({"reference": reference, "app": appId});
      } else {
        this.$location.path("/reserve").search({"reference": reference});
      }
      return;
    }

    public launchApp = (event: angular.IAngularEvent, params: Interfaces.IPhotonLaunchAppEventParam) => {
        this.gotoReserveInstancePage(params.reference, params.appId);
    };

  } // end of class
} // end of module
angular.module(Constants.Photon.MODULE_CONTROLLER_PRE_SESSION)
  .controller(Controllers.CatalogController.controllerName, Controllers.CatalogController)

测试:

describe("Catalogs controller", function() {

  beforeEach( function() {
    angular.mock.module("photonServicesCommons");
  });


  var $controller;
  var $rootScope;
  var $scope;
  var createController;
  var window;
  var $location;
  var loggerService;
  var catalogService;
  var feedbackService;
  var controller;

  beforeEach(
    inject( function(
      _$controller_,
      _$rootScope_,
      _$location_
    ) {

      $controller = _$controller_;
      $rootScope = _$rootScope_;
      $location = _$location_;
      loggerService = jasmine.createSpyObj('logger', ['error', 'info']);
      catalogService = jasmine.createSpyObj('catalogService', ['getCatalog']);
      feedbackService = jasmine.createSpyObj('feedbackService', ['']);
      window = jasmine.createSpyObj('window', ['']);
      $scope = $rootScope.$new();

      controller = $controller("CatalogController", {
        $scope: $scope,
        $location: $location,
        $window: window,
        loggerService: loggerService,
        catalogService: catalogService,
        feedbackService: feedbackService
      });
    })
  );


  it("Should init", function() {
    // var catalogController = null;
    console.log("testing window: " + $window.siteConfig );
    console.log("test: " + loggerService);
    // catalogController = createController();

    // Just want to see if the controller is created.
    expect(controller).not.toBe(null);
  });
});

0 个答案:

没有答案