简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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