无法为树组件编写单元测试用例

时间:2018-09-05 12:01:14

标签: angular unit-testing jasmine

我正在使用茉莉花编写Angular树组件的单元测试用例。我正在遵循https://angular2-tree.readme.io/docs在我的应用程序中生成树形视图。

下面是我的组件代码Permissionv2.component.ts

import { AppTranslationService } from "../../services/app-translation.service";
import { ITreeOptions, IActionMapping, TreeComponent } from 'angular-tree-component';
@Component({
    selector: 'permissionsV2-editor',
    templateUrl: './permissionsV2-editor.component.html',
    styleUrls: ['./permissionsV2-editor.component.css'],
    encapsulation: ViewEncapsulation.None
})


export class PermissionsV2EditorComponent  {
options: ITreeOptions = {
        getChildren: this.getChildren.bind(this)
    };

//Other implementation
}

下面是我的Permissionv2.component.spec.ts

import { ITreeOptions, IActionMapping, TreeComponent } from 'angular-tree-component';
import { TreeModule } from 'angular-tree-component';

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [
                TreeModule,
                HttpClientModule,
                RouterTestingModule,
                TranslateModule.forRoot({
                    loader: {
                        provide: TranslateLoader,
                        useClass: TranslateLanguageLoader
                    }
                }),
                NgxDatatableModule,
                FormsModule,
                UiSwitchModule,
                TooltipModule.forRoot(),
                ModalModule.forRoot(),
            ],
            declarations: [
                TreeComponent,
                PermissionsV2EditorComponent,
                SearchBoxComponent
            ],
            providers: [
                {
                    provide: LogMessages, useClass: LogMessagesMock
                }
  }).compileComponents();

        fixture = TestBed.createComponent(PermissionsV2EditorComponent);
        component = fixture.componentInstance;
        submitEl = fixture.debugElement;
        fixture.detectChanges();
    }));

   it('should create the Permission V2 component', async(() => {
        component.nodes = [];
        expect(component).toBeTruthy();
    }));

运行测试用例时,我遇到了错误。

  

PermissionsV2EditorComponent应该创建Permission V2组件   失败:类型TreeComponent是2个模块的声明的一部分:   TreeModule和DynamicTestModule!请考虑移动TreeComponent   到导入TreeModule和DynamicTestModule的高级模块。您   也可以创建一个新的NgModule来导出并包含TreeComponent   然后将该NgModule导入TreeModule和DynamicTestModule。

有人可以帮我弄清楚这个问题吗?任何帮助,将不胜感激。谢谢。

0 个答案:

没有答案