测试角模板驱动形式

时间:2018-06-15 23:43:06

标签: angular forms jasmine angular-forms angular-test

我正在使用angular 5,表单的@ViewChild('myForm') myForm: NgForm;属性始终为空。不确定为什么在测试过程中,角度不会通过读取模板文件来创建表单控件。

模板文件

{}

组件文件

fixture.debugElement.componentInstance.myForm.controls

在我的测试中,当我在console.log中进行以下操作时,我得到null

VALID

表单也有错误:FormControl和状态ViewChild,尽管表单应包含无效的FormBuilder。是否可以在使用ViewChild而不是componentInstance.myForm.controls配置测试表单时对其进行单元化?有没有办法通过{{1}}设置来实现?

或者我们是否必须通过使用FormGroup再次创建控件来模拟表单并将其分配给

{{1}}?

2 个答案:

答案 0 :(得分:2)

尽管我在beforeEach()中配置了testbed,但在async中却缺少异步操作。

也使用@ViewChild('nameOfForm') sampleForm: NgForm;

在测试运行规范时,模板中指定的表单控件会被初始化并在component.sampleForm中可用

答案 1 :(得分:1)

我认为你需要在检查控件之前放置fixture.detectChanges()。