单元测试typescript指令模板karma-jasmine,html未定义

时间:2016-03-09 15:06:19

标签: unit-testing typescript jasmine karma-runner angular-directive

最近,我开始使用karma-jasmine对我的打字稿代码进行单元测试。在为服务和简单​​指令创建和运行测试用例之后,我为自定义指令创建了一个测试用例,该指令有一个控制器(注入一个服务)并使用4个范围变量与外界通信。

这是一个简单的单元测试用例,用于检查指令是否正在呈现其模板。

在运行此单元测试用例时,karma会抛出一些错误

09 03 2016 19:59:27.056:INFO [framework.browserify]: bundle built
09 03 2016 19:59:27.063:INFO [karma]: Karma v0.13.21 server started at http://localhost:9876/
09 03 2016 19:59:29.964:INFO [Chrome 49.0.2623 (Linux 0.0.0)]: Connected on socket /#4OCi116hP6TDqCsmAAAA with id manual-1348
LOG: Object{0: <normal-directive></normal-directive>, length: 1}
Chrome 49.0.2623 (Linux 0.0.0) normal should render the template FAILED
Error: [$injector:unpr] Unknown provider: FacadeServiceProvider <- FacadeService
http://errors.angularjs.org/1.5.0/$injector/unpr?p0=FacadeServiceProvider%20%3C-%20FacadeService

//some reference to file
TypeError: Cannot read property 'html' of undefined
    at Object.<anonymous> (/tmp/5c59a59c62f48798a123b52b0468515b.browserify:476:23

在调试时,我知道它正在考虑&#34; normal-directive&#34;作为普通文本而不是html标签。

正常directive.spec.ts

import {appName} from '../../../../main';
import NormalController from '../../../../components/normalManager/normalList/NormalController';

describe('normalManager.normalList', () => {
    let $compile:angular.ICompileService,
        $rootScope:any,
        template:angular.IAugmentedJQuery,
        element:angular.IAugmentedJQuery,
        controller:NormalController,
        controllerSpy:jasmine.Spy;

    beforeEach(() => {
        angular.mock.module(appName);

        inject((_$compile_:ng.ICompileService, _$rootScope_:ng.IRootScopeService) => {
            $compile = _$compile_;
            $rootScope = _$rootScope_;
        });

        template = angular.element('<div normal-directive></div>');
        element = $compile(template)($rootScope);//getting error on this line.
        controller = element.controller('normalList');

        $rootScope.$digest();
    });

    it('should render the component', () => {
        expect(element.html()).toContain('<!-- normalManager.normalList -->');
    });
});

正常directive.ts

import * as angular from 'angular';
import {normalController} from './normalController';
import {html} from './normal.html'

module normal {
    "use strict";
    export class normal {
        public link: (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) => void;
        public template = html;
        public scope = {
            ngNormalVariables: '=',
            ngNormalData: '=',
            ngDifferentType: '=',
            ngType: '='
        };
        public restrict: string = 'EA';
        public controller =  normalController;
        public controllerAs: string =  'vm';
        public bindToController:boolean =  true;

        constructor() {
            normal.prototype.link = (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) => {

            };
        }

        public static Factory() {
            var directive = () => {
                return new normal();
            };

            directive['$inject'] = [];
            return directive;
        }
    }
}

export default normal;

normalController.ts

import {IfcNormalFacadeService} from '../../../normal_core/services/NormalFacadeService/IfcNormalFacadeService'
export class normalController {
    //Variable injection
    private normalFacadeService: IfcNormalFacadeService;

    public xVariableVal = null;
    public yVariableVal = null;

    //Scope variables
    private ngNormalData = {x:null, y:null, t:null, z:null};
    private ngNormalVariables = {x: [], y:[], t:[], z:[]};
    private ngType = null;
    private ngDifferentType = null;

    constructor(normalFacadeService: IfcNormalFacadeService) {
        console.log("Inside Normal controller");
        this.normalFacadeService = normalFacadeService;
    }

    ....//Remaining code
}

我正在引用this repo来编写测试用例和typescript自定义指令代码。

如果您需要更多信息,请告诉我。如果您知道任何具体的博客/网站,以了解有关打字稿的karma-jasmine单元测试的更多信息,请告诉我。

感谢你花了宝贵的时间阅读它。

问候

的Ajay

1 个答案:

答案 0 :(得分:1)

我需要为facadeService做一个模拟。

演示mocks.ts

import {IfcFacadeService} from 'filePath'

export const facadeServiceMock: IfcFacadeService {

    methodName: (): any => {}; 

}

要使用此模拟,请将其导入

演示.html.spec.ts

import {facadeServiceMock} from 'mockPathName'

beforeEach(()=>{
angular.mock.module(appName), {FacadeService: facadeServiceMock});
})