[C#]打字稿角茉莉 + 业力测试范围

标签: AngularJS C#
发布时间: 2017/2/24 2:50:10
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我小时试图弄清楚。我写了简单的角应用程序和正在测试所有类型脚本中。

控制器︰

 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 TypeError: 未定义不是一个对象 (评估 '$scope.allProducts.length') 为什么是?

解决方法 1:

你返回结果的 getAllProducts 作为一个数组︰

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

在那里你的控制器中你期望这将被分配到响应的数据属性︰

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

所以,要解决此问题,您需要更改您的测试来解决像这样︰

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

更新

解决在角的承诺是在消化循环中,这是您将需要手动触发执行的︰

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