【发布时间】:2018-10-10 04:30:12
【问题描述】:
我在Angular 6 中编写了我的第一个 Angular 应用程序。
我还没有编写任何测试,但是在生成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">
我有一个 account 模块,其中包含 ChangeAvatarModalComponent。
我在 account.module.ts
中有以下几行@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
RouterModule.forChild(AccountRoutes),
NgbModule
],
declarations: [
ChangeAvatarModalComponent
],
entryComponents: [
ChangeAvatarModalComponent
]
})
export class AccountModule { }
还有FormsModule和ReactiveFormsModule被导入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