Angular 2 testcomponentbuilder不适用于来自第三方库的多个指令

时间:2016-06-15 18:54:23

标签: angular unit-testing typescript jasmine

我正在使用Jasmine框架为我的角度2组件编写单元测试,并且如果我包含的指令不是来自角度原始模块,那么Jasmine总是跳过我的规范(在我的情况下,测试仅适用于指令,例如来自角度/核心和角/通用)。这是我的应用程序组件:

import {Component, Input, OnInit} from '@angular/core';
import {NgStyle} from '@angular/common';
import { CollapseDirective } from 'ng2-bootstrap/components/collapse';
import {MdButton} from '@angular2-material/button';
import { MdDataTable } from 'ng2-material';
import { EntityFilterPipe } from './entity-filter.pipe';
import { NgFor } from '@angular/common';
@Component({
    selector: 'test',
    templateUrl: 'app/entity.html',
    pipes: [EntityFilterPipe],
    directives: [NgStyle, NgFor, CollapseDirective]
})
export class EntityTestComponent implements OnInit {
    public title = 'Entities';
    @Input() name:string;
    showSearch: boolean = true;
    listFilter:string;
    toggleSearch(): void {
        this.showSearch = !this.showSearch;
    }
    ngOnInit() {
        console.log("printed");
    }
}

这是我的规格:

import {describe,
    it,
    beforeEach,
    inject,
    expect,
    beforeEachProviders
} from '@angular/core/testing';

import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing';
import { EntityTestComponent } from './entity-test.component';

export function main() {
    describe('Entity component', () => {
        let tcb: TestComponentBuilder;
        beforeEachProviders(() => [TestComponentBuilder, EntityTestComponent]);
        beforeEach(inject([TestComponentBuilder], (_tcb: TestComponentBuilder) => {
            tcb = _tcb
        }));

        it('should display no item found when item is not on the list', done => {
            //expect(true).toBe(true);
            tcb.createAsync(EntityTestComponent).then(fixture => {
                let property = fixture.componentInstance;
                expect(property.showSearch).toBe(true);
                done();
            })
                .catch(e => done.fail(e));
        });

如果我在列表指令中只包含NgStyle和NgFor,则运行规范。当我尝试包含来自不同库的任何内容时,例如CollapseDirective或MdDataTable,跳过规范 - 茉莉花测试不会显示成功或失败。任何建议都非常感谢! 感谢

0 个答案:

没有答案