代码之家  ›  专栏  ›  技术社区  ›  Anuj TBE

ng测试给出了组件应该创建的

  •  1
  • Anuj TBE  · 技术社区  · 6 年前

    我已经写了我的第一个角度应用程序 Angular 6

    我还没有写任何测试,但有一些默认的测试文件自动创建,同时生成 components services .

    ng test
    

    错误太多了。其中一个错误是

    ChangeAvatarModalComponent should create
    
    Failed: Template parse errors:
    There is no directive with "exportAs" set to "ngForm" ("
    <div class="modal-body">
    
      <form [formGroup]="changeAvatarForm" id="avatar-form" [ERROR ->]#formDir="ngForm" (submit)="onSubmit()">
      <div class="row">
        <div class="col-md-12">
    "): ng:///DynamicTestModule/ChangeAvatarModalComponent.html@8:56
    Can't bind to 'formGroup' since it isn't a known property of 'form'. ("
    <div class="modal-body">
    

    我有一个 模块具有 .

    我有下面几行

    @NgModule({
      imports: [
        CommonModule,
        FormsModule,
        ReactiveFormsModule,
        RouterModule.forChild(AccountRoutes),
        NgbModule
      ],
      declarations: [
        ChangeAvatarModalComponent
      ],
      entryComponents: [
        ChangeAvatarModalComponent
      ]
    })
    export class AccountModule { }
    

    还有 FormsModule ReactiveFormsModule 都是进口的 应用模块ts

    在生成的日志中有许多这样的错误。

    编辑2:change-avatar-modal.component.spec.ts

    import { async, ComponentFixture, TestBed } from '@angular/core/testing';
    
    import { ChangeAvatarModalComponent } from './change-avatar-modal.component';
    
    describe('ChangeAvatarModalComponent', () => {
      let component: ChangeAvatarModalComponent;
      let fixture: ComponentFixture<ChangeAvatarModalComponent>;
    
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ ChangeAvatarModalComponent ]
        })
        .compileComponents();
      }));
    
      beforeEach(() => {
        fixture = TestBed.createComponent(ChangeAvatarModalComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });
    
      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   danday74    6 年前

    你不是在展示你 .spec.ts

    表单出现问题的原因是,在spec文件中,也需要像这样导入相关模块:

    describe('ExampleComponent', () => {
      let component: ExampleComponent
      let fixture: ComponentFixture<ExampleComponent>
    
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          imports: [
            RouterTestingModule,
            TranslateModule.forRoot({
              loader: {provide: TranslateLoader, useClass: TranslateFakeLoader}
            }),
            HttpClientModule,
            HttpClientTestingModule,
            FormsModule,
            SfFormModule,
            ReactiveFormsModule,
            NguiAutoCompleteModule,
            NgxMyDatePickerModule,
            NgxPermissionsModule.forRoot(),
            PipeModule,
            StoreModule.forRoot({}),
            LayoutsModule
          ],
          declarations: [
            ExampleComponent
          ],
          providers: [
            {provide: APP_BASE_HREF, useValue: '/'},
            {provide: ToastrService, MockToastrService},
            ActionsSubject,
            SimService
          ]
        }).compileComponents()
      }))
    
      beforeEach(() => {
        fixture = TestBed.createComponent(ExampleComponent)
        component = fixture.componentInstance
        fixture.detectChanges()
      })
    
      it('should create', () => {
        expect(component).toBeTruthy()
      })
    })
    

    在这种情况下,您需要在spec文件中导入FormsModule和/或ReactiveFormsModule以及prob其他内容。

    为了减少导入的数量,您可以将自己的模块导入spec文件(例如AccountModule和/或AppModule),因为它们已经导入了表单内容。

    推荐文章