小编Che*_*ess的帖子

如何在Angular的构造函数中使用@Inject文本测试组件

Angular 6,我在组件的构造函数中声明了一些注入的变量,但是我不知道如何在单元测试文件中配置注入的值,当我运行时ng test,它给出了以下错误:

错误:StaticInjectorError(DynamicTestModule)[title]:
StaticInjectorError(Platform: core)[title]: NullInjectorError: No provider for title!

//我的组件

export class ConfirmModalComponent extends BaseModal implements OnInit {
  choosedCandidates: Array<any> = [];

  constructor(
      @Inject('title') public title,//injected variable
      protected modalService: ModalService
  ) {
      super();
  }

  ngOnInit() {
  }
  ....
}
//spec file of my component
beforeEach(async(() => {
  TestBed.configureTestingModule({
      declarations: [ DelInterviewerConfirmModalComponent ],
      imports: [ CheckboxModule, TranslateModule ],
      providers: [ 
        { provide: title, useValue: 'modal title' },
        ModalService, 
        RequisitionDetailService ],
    schemas: [ CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA ],
  })
.compileComponents(); …
Run Code Online (Sandbox Code Playgroud)

unit-testing angular

5
推荐指数
1
解决办法
1618
查看次数

标签 统计

angular ×1

unit-testing ×1