Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 437
Michael Avatar asked Aug 31 '25 18:08

Michael


1 Answers

Apparently I can set the value directly without a spy or mock:

   component.form = {value: {yearsWithEmployer: '6'}};
   expect(component.showPreviousEmployer()).toBe(false);
like image 64
Michael Avatar answered Sep 02 '25 08:09

Michael