2015-09-14 55 views
0

我花了数小时试图弄清楚这一点。我写了简单的Angular应用程序,现在我试图在类型脚本中测试它。TypeScript Angular Jasmine + Karma测试范围

控制器:

export class ProductsController {  
    static $inject = ["ProductService", "$scope"]; 
    constructor(public productsServices: AngularTest.Interfaces.IProductsService, $scope: any) { 


      productsServices.getAllProducts().then((response: ng.IHttpPromiseCallbackArg<AngularTest.Interfaces.IProducts[]>): any => { 
      $scope.currentPage = 1; 
      $scope.allProducts = <AngularTest.Interfaces.IProducts[]> response.data 

        $scope.cartItems = []; 
        $scope.modalAlerts = []; 

        $scope.maxItems = 3; 
        $scope.totalItems = $scope.allProducts.length; 

        $scope.itemsOnPage = $scope.allProducts.slice(0, $scope.maxItems); 
     }); 

     $scope.pageChanged = function() { 
      $scope.itemsOnPage = $scope.allProducts.slice(($scope.currentPage - 1) * $scope.maxItems, $scope.currentPage * $scope.maxItems); 
     }; 
    } 
} 

和测试:

describe("TestService",() => { 

    var mock: ng.IMockStatic; 
    var $httpBackend: ng.IHttpBackendService; 
    var service: AngularTest.Services.ProductServices; 
    var rootScopeFake; 
    var controller; 
    var $controller: ng.IControllerService; 
    var mockServiceProvider; 
    var prop: ng.IPromise<AngularTest.Interfaces.IProducts[]>; 
    var q :ng.IQService; 

    mock = angular.mock; 

    beforeEach(mock.module('app.AngularTS')); 

    beforeEach(() => mock.inject(function (_$httpBackend_, $injector, $rootScope, _$controller_, $q) { 
     $httpBackend = _$httpBackend_; 
     rootScopeFake = $rootScope.$new();  
     service = $injector.get('ProductService'); 
     $controller = _$controller_; 
     q = $q; 
    })); 

    afterEach(function() { 
     $httpBackend.verifyNoOutstandingExpectation(); 
     $httpBackend.verifyNoOutstandingRequest(); 
    }); 


    it("Should Call API", function() { 

     spyOn(service, "getAllProducts").and.callFake(() => { 
      var deffered; 
      deffered = q.defer(); 
      deffered.resolve(['xxxx', 'xxxx']); 
      return deffered.promise; 
     }); 

     controller = new AngularTest.Controllers.ProductsController(service, rootScopeFake); 
     expect(service.getAllProducts).toHaveBeenCalled(); 
     expect(rootScopeFake.allProducts).toBeDefined(); 

    }); 
}); 

当我尝试测试我的ProductsController我recive类型错误: '未定义' 不是(评估“$ scope.allProducts.length对象') 这是为什么 ?

回答

0

您正在返回的getAllProducts结果作为数组:

deffered.resolve(['xxxx', 'xxxx']); 

凡在你的控制器,你期待这被分配到响应的数据属性:

$scope.allProducts = <AngularTest.Interfaces.IProducts[]> response.data 

所以,要解决这个问题,你需要改变你的测试,像这样解决:

deffered.resolve({data: ['xxxx', 'xxxx']}); 

UPDATE

的解决角度是消化循环,这是一件好事内执行承诺的,您将需要手动触发:

controller = new AngularTest.Controllers.ProductsController(service, rootScopeFake); 
rootScopeFake.$digest(); // <-- this is the important part 
expect(service.getAllProducts).toHaveBeenCalled(); 
+0

我像你说的已经做了不完全,但仍然有问题。基本上我不能访问角度控制器 - 构造器内的任何范围值。例如,如果我试图做期望(rootScopeFake.currentPage).toEqual(1);我recive错误:预计未定义等于1. –

+0

我添加了一个更新来处理该情况...调用您的范围 – Brocco

+0

$摘要,你是男人。我已经浪费了一天的时间。非常感谢你 –

相关问题