2014-10-16 109 views
0

当我在Jasmine中运行我的测试时出现该错误。我正在尝试创建一个模拟ajax调用的测试。数据本身并不重要,我只想看看测试运行。我的代码:TypeError:无法读取未定义的属性'_scope'

export interface IMockScope extends ng.IScope { 
 
     promotion: any; 
 
     prometricId: string; 
 
    } 
 

 
    export class MockController { 
 

 
     private _mockService: MockService; 
 
     private _scope: IMockScope; 
 

 
     constructor($scope: IMockScope, mockService: MockService) { 
 

 
      // assigning scope and service 
 
      this._mockService = mockService; 
 

 
      if ($scope === undefined) { 
 
       throw ("exception"); 
 
      } 
 
      if (typeof($scope) === "MockController") { 
 
       console.log("I am the right type"); 
 
      } 
 
      this._scope = $scope; 
 

 
      // getting a prometric ID for the promotion to load 
 
      this._scope.prometricId = "xxx"; 
 

 

 
      // loading a promotion through a service call 
 
      this._mockService.GetPromotion(this._scope.prometricId).then(function (data: any) { 
 
       this._scope.promotion = data; 
 
      }, null); 
 

 
     } 
 
    } 
 

 
    export class MockService { 
 

 
     private _$http: ng.IHttpService; 
 
     private _$q: ng.IQService; 
 

 
     constructor($http: ng.IHttpService, $q: ng.IQService) { 
 
      this._$http = $http; 
 
      this._$q = $q; 
 
     } 
 

 
     GetPromotion(prometricId: string): ng.IPromise<any> { 
 

 
      var deferred = this._$q.defer(); 
 
      var data = { 
 
       prometricId: prometricId 
 
      }; 
 

 
      var promotion: any; 
 

 
      this._$http.get("/PreEval/GetPromotion").then(function (data: any) { 
 
       promotion = data; 
 
       deferred.resolve(promotion); 
 
      }, null); 
 

 
      return deferred.promise; 
 
     } 
 
    } 
 

 
    var mockApp = angular.module('mockApp', []); 
 
    //mockApp.controller('MyController', MyController); 
 
    mockApp.controller('MyController', ["$scope", "mockService", function ($scope: IMockScope, mockService: MockService) { 
 
     return new MockController($scope, mockService); 
 
    }]); 
 

 
    mockApp.factory("mockService", ["$http", "$q", function ($http: ng.IHttpService, $q: ng.IQService) { 
 
     return new MockService($http, $q); 
 
    }]);

我的测试中茉莉:

describe("Mock Controller", function() { 
 

 
     var controller: MockController; 
 
     var scope: IMockScope; 
 
     var $http: ng.IHttpService; 
 
     var $q: ng.IQService; 
 
     var mockService: MockService; 
 
     var backEnd: ng.IHttpBackendService; 
 

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

 
     beforeEach(angular.mock.inject(function ($rootScope: ng.IRootScopeService, _$http_: ng.IHttpService, _$q_: ng.IQService, 
 
            _mockService_: MockService, $httpBackend: ng.IHttpBackendService) { 
 

 
      scope = <IMockScope> $rootScope.$new(); 
 
      $http = _$http_; 
 
      $q = _$q_; 
 
      mockService = _mockService_; 
 
      backEnd = $httpBackend; 
 

 
      backEnd.when("GET", "/PreEval/GetPromotion").respond(
 
       { 
 
        "Success": true, 
 
        "ErrorMessage": "", 
 
        "Result": [ 
 
         { "ContactTypeId": 2, "Name": "Coworker" }, 
 
         { "ContactTypeId": 3, "Name": "Family" }, 
 
         { "ContactTypeId": 5, "Name": "Fresh" }, 
 
         { "ContactTypeId": 1, "Name": "Friend" } 
 
        ] 
 
       }); 
 

 
     })); 
 

 

 
     it("should have data",() => { 
 
      
 
      controller = new MockController(scope, mockService); 
 
      backEnd.flush(); 
 
      expect(scope.promotion).toBeDefined(); 
 
     }); 
 

 
    }); 
 
}

我试着寻找类似的问题,但什么也没有帮助。任何建议表示赞赏。

回答

1
 this._mockService.GetPromotion(this._scope.prometricId).then(function (data: any) { 
      this._scope.promotion = data; 
     }, null); 

你在这里失去this上下文的功能。改为使用箭头功能:

 this._mockService.GetPromotion(this._scope.prometricId).then(data => { 
      this._scope.promotion = data; 
     }, null); 
+0

这很完美!谢谢 – 2014-10-16 18:53:13

相关问题