【发布时间】:2019-01-06 10:59:42
【问题描述】:
我正在尝试测试NgForm 并检查是否在商店状态更新时设置了表单的值。
@ViewChild('form') form: NgForm;
ngOnInit() {
this.subscription = this.store.select('shoppingList').subscribe(data => {
this.editedItem = data.editedIngredient;
this.form.setValue({
name: this.editedItem.name,
amount: this.editedItem.amount
})
});
}
但是在设置我得到的值时
There are no form controls registered with this group yet.
If you're using ngModel, you may want to check next tick (e.g. use setTimeout).
还尝试创建假表单并设置它而不是NgForm
TestBed.createComponent(ShoppingEditComponent).debugElement.componentInstance.form = { value: {}, setValue: (newValue) => { this.value = newValue }};
但在测试中它的值永远不会更新,变成空的value 对象。
测试这种情况的最佳方法是什么?
【问题讨论】:
标签: javascript angular unit-testing testing