我有一个组件,其模板如下所示:
// Template
<form #f="ngForm" (ngSubmit)="onFormSubmit(f)">
<!-- A bunch of form fields -->
</form>
我的组件有一个方法:
onFormSubmit(form: NgForm) {
this.save();
}
我想编写一个测试,其基本外观如下,测试当表单提交时 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)."