0

我正在单元测试一个包含clarity带有clarit directives.

我已经模拟了清晰度标签,但它们具有clrDgItems我无法用指令类模拟的指令。

<clr-dg-row *clrDgItems="let item of items$ | async | filter : listFilter.keyword : ['trackingCode', 'title']" [clrDgItem]="episode">

我可以替换clrDgItemsngFor但表格过滤器停止工作。

没有它,测试将无法编译:

无法绑定到“clrDgItemsOf”,因为它不是“clr-dg-row”的已知属性

当我将 mockDirective 添加为:我收到错误

失败:模块“DynamicTestModule”声明的意外值“[object Object]”

     function MockDirective(options: any): Directive {
         const metadata: Directive = {
         selector: options.selector,
         inputs: options.inputs,
         outputs: options.outputs
      };
      return new Directive(metadata);
    }

 TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        MockDirective({
          selector: '[clrDgItemsOf]',
        }),
      MockComponent({
      selector: 'clr-dg-row',
      template: '<ng-content></ng-content>',
      outputs: ['clrDgItemsOf'],
      inputs: ['clrDgItem']
    }), ...

有什么建议么?

4

1 回答 1

4

您不使用的任何原因CUSTOM_ELEMENTS_SCHEMA?你不需要模拟指令,因为这些指令会被忽略:

TestBed.configureTestingModule({
  imports: [
    RouterTestingModule,    
  ],
  schemas: [
    CUSTOM_ELEMENTS_SCHEMA
  ]
})

如果你真的想模拟行为,你可以@Directive在你的规范文件中创建一个:

@Directive({
  selector: '[clrDgItems][clrDgItemsOf]'
})
export class MockClrDgItemsDirective {
  @Input()
  clrDgItemsOf: any;
}

并像往常一样将其添加到您的声明中:

TestBed.configureTestingModule({
  imports: [
    RouterTestingModule
  ],
  declarations: [
    MockClrDgItemsDirective
  ]
})

甚至另一种选择是只ClrDatagridModule在您的导入中导入TestBed

于 2019-07-18T16:43:17.930 回答