Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.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
Angular6 如何在编写测试用例时减少重复代码_Angular6_Angular Test - Fatal编程技术网

Angular6 如何在编写测试用例时减少重复代码

Angular6 如何在编写测试用例时减少重复代码,angular6,angular-test,Angular6,Angular Test,我在我的app component.spec.ts的每个description部分都写了大量的锅炉代码和重复代码。有没有办法减少它?另一个问题是,每当我编写一个新的组件,我都必须在每个描述中明确添加新组件的引用。例如 describe('AppComponent Test suite', () => { let component: AppComponent; let fixture: ComponentFixture<AppComponent>

我在我的
app component.spec.ts
的每个
description
部分都写了大量的锅炉代码和重复代码。有没有办法减少它?另一个问题是,每当我编写一个新的
组件
,我都必须在每个
描述
中明确添加新
组件
的引用。例如

    describe('AppComponent Test suite', () => {

      let component: AppComponent;
      let fixture: ComponentFixture<AppComponent>;

      beforeEach((() => {
        TestBed.configureTestingModule({
          declarations: [
            AppComponent,
    ... //29 components need to be referrred her
          ],
          imports: [
            AppRoutingModule,
            QuillModule,
            BrowserModule,
            HttpClientModule,
            MatProgressBarModule,
            BrowserAnimationsModule,

           HttpClientXsrfModule.withOptions({cookieName: 'CJCsrfCookie', headerName: 'CJCsrfHeader'}),
            ReactiveFormsModule
          ],
          providers: [{provide: APP_BASE_HREF, useValue: '/'},
    ...//14 services need to be added here
        }).compileComponents();
      }));

      beforeEach(() => {
        fixture = TestBed.createComponent(AppComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });

      fit('should create the app', (async() => {

        expect(component).toBeTruthy();
      }));
...
});
description('AppComponent测试套件',()=>{
let组件:AppComponent;
let夹具:组件夹具;
在每个((()=>{
TestBed.configureTestingModule({
声明:[
应用组件,
…//29个组件需要向她推荐
],
进口:[
批准模块,
纬管模块,
浏览器模块,
HttpClientModule,
MAT模块,
BrowserAnimationsModule,
HttpClientXsrfModule.withOptions({cookieName:'CJCsrfCookie',headerName:'CJCsrfHeader'}),
反应窗体模块
],
提供者:[{provide:APP_BASE_HREF,useValue:'/'},
…//此处需要添加14个服务
}).compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(AppComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
fit('应该创建应用',(async()=>{
expect(component.toBeTruthy();
}));
...
});

如果我再写一个
descripe
,我就必须再写一次所有的
导入
声明
。我能避免吗?

除非我弄错了,否则做起来似乎很容易

我只需导入
AppModule
。对于任何
提供程序
我想提供的替代,我只需明确提到这一点

例如

以上取代

beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [//23 components. These in new implementation come from AppModule
      ],
      imports: [AppModule],
      providers: [//14 services. They also some now from AppModule,
        {provide: UserManagementService, useClass: MockUserManagementService}, //mock user management service
        {provide: APP_BASE_HREF, useValue: '/'}],
    })
    .compileComponents();
  }));
beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [//23 components. These in new implementation come from AppModule
      ],
      imports: [AppModule],
      providers: [//14 services. They also some now from AppModule,
        {provide: UserManagementService, useClass: MockUserManagementService}, //mock user management service
        {provide: APP_BASE_HREF, useValue: '/'}],
    })
    .compileComponents();
  }));