[英]DynamicTestModule throws error on angular unit testing
I'm currently creating a unit testing with Karma and Jasmine as:我目前正在使用 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);
}))
});
The profile list component has many childs, which is why I imported them on declarations
.配置文件列表组件有很多子项,这就是我在
declarations
中导入它们的原因。 The ProfilesFiltersComponent
uses angular Forms as: ProfilesFiltersComponent
使用 angular Forms 作为:
HTML: HTML:
TS TS
export class ProfilesFiltersComponent implements OnInit {
filterForm: FormGroup;
}
So, the testing asking me for the form group as:因此,测试要求我提供表单组:
Can't bind to 'formGroup' since it isn't a known property of 'form'
无法绑定到“formGroup”,因为它不是“form”的已知属性
So I added it to declarations
on the testing as:所以我将它添加到测试
declarations
中:
declarations: [
...
FormGroup
]
But now it is throwing a new error:但现在它抛出一个新错误:
Failed: Unexpected value 'FormGroup' declared by the module 'DynamicTestModule'.
失败:模块“DynamicTestModule”声明的意外值“FormGroup”。 Please add a @Pipe/@Directive/@Component annotation.
请添加 @Pipe/@Directive/@Component 注解。
That I have no idea how to solve;我不知道如何解决; what am I doing wrong?
我究竟做错了什么?
Remove FormGroup
from declarations
.从
declarations
中删除FormGroup
。
Try changing the imports to this:尝试将导入更改为此:
imports: [
RouterTestingModule,
// You either need FormsModule or ReactiveFormsModule
FormsModule,
ReactiveFormsModule
],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.