Reputation: 1515
In my angular app, I am writing a testcase for a component which has @Input value. How can I mock the @Input value. The main component's testSave() method uses subComponent's InputObject's id. When I run the testcase it says undefined is an object at 'this.subComponent.InputObject.id;'
export class SubComponent implements OnInit {
@Input() inputObj: InputObject;
}
export class MainComponent implements OnInit {
@Input() subComponent: SubComponent;
testsave() {
this.subComponent.InputObject.id;
}
}
export class InputObject {
contructor(id:string, name: string)
}
TestCase:
it('should save', fakeAsync(() => {
// const event: MockEvent = new MockEvent();
// fixture = TestBed.createComponent(MainComponent);
// component = fixture.componentInstance;
});
Upvotes: 3
Views: 7797
Reputation: 2416
Since subComponent
and inputObj
are public you can simply override it, if you test MainComponent
you can have something like:
component.subComponent = {inputObj: {id: 'someId', name :'someName'}};
Upvotes: 6