【问题标题】:ng test gives Component should createng 测试给出组件应该创建
【发布时间】:2018-10-10 04:30:12
【问题描述】:

我在Angular 6 中编写了我的第一个 Angular 应用程序。

我还没有编写任何测试,但是在生成componentsservices 时会自动创建一些默认测试文件。

当我使用

运行自动生成的测试时
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">

我有一个 account 模块,其中包含 ChangeAvatarModalComponent

我在 account.module.ts

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

还有FormsModuleReactiveFormsModule被导入app.module.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();
  });
});

【问题讨论】:

    标签: angular angular6 angular-test


    【解决方案1】:

    您没有在您提供的代码中显示.spec.ts 文件。

    您遇到表单问题的原因是因为在您的规范文件中您也需要像这样导入相关模块:

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

    在您的情况下,您需要在规范文件中导入 FormsModule 和/或 ReactiveFormsModule 并探测其他内容。

    要减少导入的数量,您可能只需将自己的模块导入规范文件即可 - 例如AccountModule 和/或 AppModule - 因为它们已经导入了表单内容。

    【讨论】:

    • 添加了规范文件。检查编辑2
    • 好酷,正如预期的那样,您在规范文件中缺少导入 - 修复它,它应该可以解决您的问题 :)
    • 我需要添加您在答案中添加的所有导入行还是仅添加我在 account.module.ts 中导入的行?
    • 只是您正在测试的特定组件所需的导入,尽管导入超过您需要的部分并没有什么坏处。您可以只导入 AccountModule,因为它会导入表单模块。您可能会发现,当您这样做时,可能会出现与其他缺失导入相关的不同错误。只需一个一个地解决它们,最终它就会停止抱怨:)
    • 我给出的答案只是规范文件的一个示例,您不需要导入大部分内容。
    猜你喜欢
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 2011-01-17
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多