3

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

1 Answer 1

6

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'}};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.