【问题标题】:TypeScript Angular Jasmine+Karma testTypeScript Angular Jasmine+Karma 测试
【发布时间】:2015-12-07 20:50:01
【问题描述】:

我花了几个小时试图弄清楚这一点。我正在尝试使用 Typescript 中的测试来测试我的 Angular 控制器(用 typeScript 编写)。我被困在将服务模拟到我的控制器中。这是代码:

控制器:

export class ProductsController {     
    static $inject = ["ProductService", "$scope"];
    constructor(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);
        };
    }
}

服务:

    module AngularTest.Services{
    class ProductServices implements AngularTest.Interfaces.IProductsService{
        httpService: ng.IHttpService

        static $inject = ["$http"];
        constructor($http: ng.IHttpService)
        {
            this.httpService = $http;

        }

        getAllProducts(): ng.IPromise<AngularTest.Interfaces.IProducts[]> {
            return this.httpService.get('/api/Angular');
            };
    }

    factory.$inject = ['$http'];
    function factory($http : ng.IHttpService) {
        return new ProductServices($http);
    }

    angular
        .module('app.AngularTS')
        .factory('ProductService', factory);

}

测试:

    describe("TestService", () => {

    var mock: ng.IMockStatic;
    var $httpBackend: ng.IHttpBackendService;
    var service; //AngularTest.Interfaces.IProductsService;
    var rootScopeFake;
    var controller;
    var $controller: ng.IControllerService;

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

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

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

    it("Should Call API", function () {
        controller = $controller('ProductsController', { ProductService: service, $scope: rootScopeFake});

        spyOn(service, "getAllProducts");

        expect($httpBackend.expectGET('/api/Angular').respond([
            { "x": "xx", "xxx": "xxxx", "xxxxx": 5 },
            { "x": "xx", "xxx": "xxxx", "xxxxx": 5.5 },
            { "x": "xx", "xxx": "xxxx", "xxxxx": 6 },
            { "x": "xx", "xxx": "xxxx", "xxxxx": 0 }
        ])).toHaveBeenCalled;

        expect(service.getAllProducts).toHaveBeenCalled();  // this fails why ?
        $httpBackend.flush();

    });
});

我不知道为什么这不起作用,我收到了。预计间谍 getAllProducts 已被调用。

【问题讨论】:

    标签: angularjs testing typescript jasmine


    【解决方案1】:

    您应该在使用您正在监视的方法之前创建您的间谍。由于该方法在控制器构造函数中使用。应该在启动控制器之前创建间谍。

    it("Should Call API", function () {        
        spyOn(service, "getAllProducts").and.callFake(function(){
            //return promise
        });
    
        controller = $controller('ProductsController', { ProductService: service, $scope: rootScopeFake});
    
        ....
    

    【讨论】:

    • 我已经做到了。现在我收到错误:TypeError:'undefined' 不是 ProductsController 的对象(评估 'productsServices.getAllProducts().then')。有什么想法吗?
    • 它期望 getAllProducts 返回一个承诺。您可以通过向您的间谍添加“and.callFake”来实现此目的。 (我编辑了我的代码)我不是 TypeScript 专家,所以我不确定如何使用 ng.IPromise 返回一个承诺。我把它留给你。 :-)
    • 仍然收到错误:TypeError: 'undefined' is not an object (evalating 'spyOn(service, "getAllProducts").and.callFake') 方法:spyOn(service, "getAllProducts").and .callFake(() => { var defered; defered = q.defer(); defered.resolve('xxxx'); return defered.promise; });
    • 你用的是哪个版本的茉莉花?
    • package.json 文件:“devDependencies”:{“grunt”:“~0.4.5”,“grunt-contrib-concat”:“^0.5.1”,“grunt-contrib-uglify ": "~0.4.0", "grunt-contrib-watch": "^0.6.1", "grunt-karma": "^0.8.3", "grunt-typescript": "^0.7.0", “业力”:“^0.12.16”,“业力-茉莉花”:“^0.1.5”,“业力-phantomjs-launcher”:“^0.2.1”,“业力铬启动器”:“~0.1 .2", "karma-xml-reporter": "^0.1.4", "phantomjs": "^1.9.7-8" }
    猜你喜欢
    • 2015-12-10
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多