【问题标题】:Angular JS Typescript Unit Testing KarmaAngular JS Typescript 单元测试业力
【发布时间】:2014-01-21 13:02:05
【问题描述】:

我目前参与了一个使用 typescript 构建其单页 anuglar 应用程序的项目。不幸的是,他们没有进行单元测试。作为使用打字稿的新手,我在进行任何测试时遇到了问题。下面是一个示例测试。然后是打字稿文件

当我运行测试时,它找不到控制器!

(function() {
'use strict';

    describe('CarController', function() {

        // Load the controllers module
        beforeEach(module('CarDealerApp'));

        var scope,
            CarController ;

        beforeEach(inject(function($controller) {
            scope = {};

            CarController = $controller('CarController', {
                $scope: scope
            });
        }));

        it('should set the car name', function() {

            expect(scope.carMake).toEqual('Ford');

        });

    });

})();


module CarDealerApp.Cars.Controllers {
    "use strict";

    export interface ICarScope extends angular.Scope {
        carName : string;
    }

    export class CarController {

        carName:string = "Ford";

        // $inject annotation
        public static $inject = [
            '$scope'
        ]

        constructor(private $scope: ICarScope){

            $scope.vm = this;
        }

    }
}

【问题讨论】:

  • 你不想期待 scope.vm.carMake 还是只做 CarController.carMake?

标签: angularjs typescript karma-runner


【解决方案1】:

虽然你在那里写的东西有效,但我建议像这样构建你的 TypeScript 控制器(在你的测试中):

CarController = new CarDealerApp.Cars.Controllers.CarController($rootScope.$new());

这是一种更简洁的结构,是编写 TypeScript 测试时的正确方法。

【讨论】:

    【解决方案2】:

    终于搞定了。

    基本上我需要像这样传递控制器的完整模块名称:

    CarController = $controller('CarDealerApp.Cars.Controllers.CarController');

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      • 2016-06-19
      • 2019-12-10
      • 2019-04-16
      • 1970-01-01
      • 2018-06-09
      相关资源
      最近更新 更多