简体   繁体   English

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

[英]How to test component with a @Inject text in the constructor in Angular

Angular 6, I declare some injected variables in the constructor of my component, But I don't know how to configure the injected value in the unit test file, when I run ng test and it gives the following error: 角度6,我在组件的构造函数中声明了一些注入的变量,但是当我运行ng test时,我不知道如何在单元测试文件中配置注入的值,并且它给出以下错误:

Error: StaticInjectorError(DynamicTestModule)[title]: 错误:StaticInjectorError(DynamicTestModule)[标题]:
StaticInjectorError(Platform: core)[title]: NullInjectorError: No provider for title! StaticInjectorError(平台:核心)[标题]:NullInjectorError:没有标题提供者!

//My component //我的组件

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();
}));

beforeEach(() => {
  fixture = TestBed.createComponent(DelInterviewerConfirmModalComponent);
  component = fixture.componentInstance;
  component.title = "test";
  fixture.detectChanges();
});

it('should create', () => {
  expect(component).toBeTruthy();
});

I wonder if anybody meets the same problem and how to resolve the errors, thanks in advance. 我想知道是否有人遇到相同的问题,以及如何解决错误,请先感谢。

Values like dates, numbers and string must be wrapped into an Injection Token . 日期,数字和字符串之类的值必须包装在Injection Token中

1.: Create an injection token: 1 .:创建注入令牌:

export const TITLE = new InjectionToken<string>('title');

2.: Inject like you do now: 2 .:像现在一样注入:

constructor(@Inject(TITLE) private title: string)

3.: In your test, mock it using the InjectionToken: 3 .:在您的测试中,使用InjectionToken模拟它:

  TestBed.configureTestingModule({
      declarations: [ DelInterviewerConfirmModalComponent ],
      imports: [ CheckboxModule, TranslateModule ],
      providers: [ 
        { provide: TITLE, useValue: 'modal title' },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将带有参数化构造函数的服务注入到组件-Angular - How to Inject a Service with Parameterized constructor to a Component - Angular 如何在构造函数中使用@Inject()为Angular 7 Service准备测试 - How to prepare a test for an Angular 7 Service that using @Inject() in the constructor Angular 2:将视图/ DOM注入组件构造函数 - Angular 2: Inject view/DOM into component constructor 如何将ts-mockito注入Angular 4组件测试 - How to inject ts-mockito into Angular 4 component test 使用构造函数参数测试Angular 2组件 - Test Angular 2 Component with Constructor Parameter 如何在 Angular 中注入具有构造函数参数的 object? - How to inject object that has constructor parameters in Angular? 如何通过对构造函数的异步调用注入服务,Angular 2 - How to Inject services with Async call on constructor, Angular 2 Angular 基于组件@Input属性的构造函数中的服务注入参数 - Angular Service inject parameter in constructor based on component @Input property 如何将自定义字符串注入Angular中的服务构造函数? - How to inject custom string into service constructor in Angular? Angular 9 - 如何将动态参数注入服务构造函数 - Angular 9 - how to inject dynamic parameters into a service constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM