Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度测试:为什么要在提供者中测试组件?_Angular_Typescript_Unit Testing - Fatal编程技术网

Angular 角度测试:为什么要在提供者中测试组件?

Angular 角度测试:为什么要在提供者中测试组件?,angular,typescript,unit-testing,Angular,Typescript,Unit Testing,在中,它有一个示例,其中被测试的组件被放置在TestBed.configureTestingModule的providers部分,与相关服务一起。然后,他们使用TestBed.get而不是TestBed.createComponent来访问组件: app/welcome/welcome.component.spec.ts(仅限类设置) 我看到的大多数其他文档和教程都将组件放在声明下 我想什么时候在提供者中对组件进行测试?为什么?在文档中提到,如果测试中的组件有一些DI,那么我们需要像这样创建组件

在中,它有一个示例,其中被测试的组件被放置在
TestBed.configureTestingModule
的providers部分,与相关服务一起。然后,他们使用
TestBed.get
而不是
TestBed.createComponent
来访问组件:

app/welcome/welcome.component.spec.ts(仅限类设置)

我看到的大多数其他文档和教程都将组件放在声明下


我想什么时候在提供者中对组件进行测试?为什么?

在文档中提到,如果测试中的
组件有一些DI,那么我们需要像这样创建组件。它将创建组件的实例,类似于创建服务实例的方式。您可以使用
TestBed.get(ComponentName)
获取该组件并测试该组件

所以,我测试了这个方法,它是有效的。。。惊人地好问题。injector将创建与服务相同的组件。
beforeEach(() => {
  TestBed.configureTestingModule({
    // provide the component-under-test and dependent service
    providers: [
      WelcomeComponent,
      { provide: UserService, useClass: MockUserService }
    ]
  });
  // inject both the component and the dependent service.
  comp = TestBed.get(WelcomeComponent);
  userService = TestBed.get(UserService);
});