我目前正在用Karma和Jasmine创建一个单元测试:
describe('ProfileListComponent', () => {
let component: ProfileListComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
[RouterModule.forRoot([])],
FormsModule
],
declarations: [
ProfileListComponent,
ProfilesFiltersComponent,
DatatableComponent,
ProfileImagePopoverComponent,
NgbHighlight,
NgbRating,
PaginationFooterComponent
],
providers: []
}).compileComponents();
TestBed.configureTestingModule({
declarations: [ ProfileListComponent ]
})
.createComponent(ProfileListComponent);
}));
it('should be 4', async(() => {
expect(component.add(2,2)).toEqual(4);
}))
});
配置文件列表组件有许多子组件,这就是我在declarations
上导入它们的原因。ProfilesFiltersComponent
使用角形式作为:
HTML:
TS
export class ProfilesFiltersComponent implements OnInit {
filterForm: FormGroup;
}
因此,测试要求我将表单组设置为:
不能绑定到'formGroup',因为它不是'form'的已知属性
所以我在测试中将其添加到declarations
中:
declarations: [
...
FormGroup
]
但是现在抛出了一个新的错误:
Failed:模块声明的非预期值'FormGroup'"DynamicTestModule"。请添加@Pipe/@Directive/@Component注释。
我不知道如何解决;我做错了什么?
将FormGroup
从declarations
中移除
尝试将导入更改为:
imports: [
RouterTestingModule,
// You either need FormsModule or ReactiveFormsModule
FormsModule,
ReactiveFormsModule
],