单元测试 typescript 指令模板 karma-jasmine,html 未定义 [英] unit testing typescript directive template karma-jasmine, html is not defined

查看:17
本文介绍了单元测试 typescript 指令模板 karma-jasmine,html 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Recently i started unit testing on my typescript code using karma-jasmine. After creating and running test case for a service and a simple directive, i created one test case for custom directive which has one controller(which is injecting one service) and is using 4 scope variable for communicating with outside world.

这是一个简单的单元测试用例来检查指令是否正在渲染它的模板.

It's a simple unit test case to check whether directive is rendering its template or not.

在运行这个单元测试用例时,业力会抛出一些错误

While running this unit test case, karma throws some error

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

在调试它时,我知道它正在将正常指令"视为普通文本而不是 html 标记.

While debugging it, i get to know that it is considering "normal-directive" as normal text not as a html tag.

normal-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 -->');
    });
});

normal-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 存储库来编写测试用例和 typescript 自定义指令代码.

I am refering this repo to write test case and typescript custom directive code.

如果您需要更多信息,请告诉我.如果你知道任何具体的博客/网站来了解更多关于 typescript 的 karma-jasmine 单元测试,请告诉我.

If you require more info, do tell me. If you know any concrete blog/site to learn more about karma-jasmine unit testing for typescript, please do tell me.

感谢您抽出宝贵的时间阅读.

Thanks for giving your precious time in reading it.

问候

阿杰

推荐答案

我需要为 facadeService 做一个 mock.

I needed to make a mock for facadeService.

演示 mocks.ts

Demo mocks.ts

import {IfcFacadeService} from 'filePath'

export const facadeServiceMock: IfcFacadeService {

    methodName: (): any => {}; 

}

要使用这个模拟,请导入它

To use this mock, import it

演示 .html.spec.ts

Demo .html.spec.ts

import {facadeServiceMock} from 'mockPathName'

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

这篇关于单元测试 typescript 指令模板 karma-jasmine,html 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆