【发布时间】:2019-05-15 10:56:45
【问题描述】:
我在 components.ts 中有这段代码
export class TimelinePlotComponent implements OnInit, OnChanges, OnDestroy {
form: FormGroup;
@Output() onchange: EventEmitter<any> = new EventEmitter<any>();
constructor() {}
initForm() {
this.form = new FormGroup({
timeRange: new FormControl(this.time_range_options_active, []),
metric: new FormControl(this.metric_options_active, []),
groupBy: new FormControl(this.group_by_options_active, []),
});
// How to unit test this part of the code
this.form.valueChanges.subscribe( res => {
console.log('form-changed');
this.onchange.emit(res);
});
}
}
component.spec.ts
fit('should listen for form changes', async() => {
component.form.controls['groupBy'].setValue('color');
fixture.detectChanges();
fixture.whenStable().then(() => {
// your expectations.
expect(component.form.valid).toBeTruthy();
component.onchange.subscribe( res => {
console.log('res: ', res);
});
});
});
错误:什么都没有发生,我不知道如何对触发输出事件发射器的表单进行单元测试。
如您所见,这不起作用,对如何对表单更改进行单元测试有任何帮助吗?
【问题讨论】:
-
我认为您需要触发变更检测:在期望之前添加
fixture.detectChanges()。 -
我试过@HodossySzabolcs :(
标签: angular unit-testing karma-jasmine