Ste*_*art 7 unit-testing jasmine angular
我有一个包含如下模板的组件:
// Template
<form #f="ngForm" (ngSubmit)="onFormSubmit(f)">
<!-- A bunch of form fields -->
</form>
Run Code Online (Sandbox Code Playgroud)
我的组件有一个方法,如:
onFormSubmit(form: NgForm) {
this.save();
}
Run Code Online (Sandbox Code Playgroud)
我想编写一个基本上看起来像这样的测试,测试在提交表单时调用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');
});
Run Code Online (Sandbox Code Playgroud)
如何创建表单的模拟版本以传递给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)."
Ale*_*kij 14
这应该工作
const testForm = <NgForm>{
value: {
name: "Hello",
category: "World"
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3297 次 |
| 最近记录: |