简体   繁体   English

如何在Jasmine中监视或模拟Angular @Input?

[英]How do I Spy or Mock an Angular @Input in Jasmine?

I have a component which has an Input, and functions that use the inputs 我有一个具有输入的组件,以及使用这些输入的函数

Component: 零件:

@Input() form: FormGroup;
....

showPreviousEmployer() {
    return parseInt(this.form.value.yearsWithEmployer, 10) < 5;
}

How Can I either Spy on or mock 'form' for jasmine tests? 如何监视或模拟“ form”进行茉莉花测试?

I tried: 我试过了:

    spy = spyOnProperty(component, 'form', 'get').and.returnValue({value: {yearsWithEmployer: '6'}});
    expect(component.showPreviousEmployer).toBe(false);

However that gives the error: 但是,这给出了错误:

Error: form property does not exist 错误:表单属性不存在

Apparently I can set the value directly without a spy or mock: 显然,我可以直接设置值而无需间谍或模拟:

   component.form = {value: {yearsWithEmployer: '6'}};
   expect(component.showPreviousEmployer()).toBe(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM