我有一个带有如下模板的组件:
// Template
<form #f="ngForm" (ngSubmit)="onFormSubmit(f)">
<!-- A bunch of form fields -->
</form>
我的组件有一个类似的方法:
onFormSubmit(form: NgForm) {
this.save();
}
我想编写一个基本上如下所示的测试,测试提交表单时是否调用保存函数:
it('saves the widget when the form is submitted', () => {
let testForm = new NgForm([], []);
testForm.setValue({
name: "Hello",
category: "World"
});
component.onFormSubmit(testForm);
// Run tests to check that the component data was updated
expect(component.data.name).toEqual('Hello');
expect(component.data.category).toEqual('World');
});
如何创建表单的模拟版本以传递给onFormSubmit()
功能?我尝试执行上述操作,但收到错误:"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)."
这应该有效
const testForm = <NgForm>{
value: {
name: "Hello",
category: "World"
}
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)