编写测试案例时如何减少重复代码和重复代码

问题描述 投票:1回答:1

我在describe的每个app-component.spec.ts部分中编写了很多锅炉代码和重复代码。有办法减少吗?另一个问题是,每当我编写一个新的component时,都必须在每个component中显式地添加对新describe的引用。例如。

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

如果我写另一个describe,则必须再次写所有importsdeclarations。我可以避免吗?

我在我的app-component.spec.ts的每个describe部分中编写了很多锅炉代码和重复代码。有办法减少吗?另一个问题是,每当我编写新组件时,都必须...

angular6 angular-testing
1个回答
1
投票

除非我弄错,否则似乎很容易做到!

© www.soinside.com 2019 - 2024. All rights reserved.