mardi 19 décembre 2017

Angular - Undefined is not an object (evaluating '_co.selectedQuestion.qType')

First of all, I'm new to Angular and testing. I tried to test the following component:

export class FillInOpenQuestionComponent implements OnInit {

  @Input() answer: string;
  @Output() updateAnswer = new EventEmitter();

  constructor() { }

  ngOnInit() {
  }

  updateOpenAnswer(value) {
      this.updateAnswer.emit(value);
  }

}

by doing the following:

it('Should not emit the event initially', () => {
    expect(htmlElement.textContent).toEqual('');
});

it('Should emit the event after update', () => {
    openQuestionComponent.updateOpenAnswer('Test value');
    openQuestionFixture.detectChanges();
    fixture.detectChanges();
    expect(htmlElement.innerHTML).toBe('Test value');
});

Once the function is called, the value of htmlElement should be 'Test value'. But as soon as I run the test, I get the error:

TypeError: undefined is not an object (evaluating '_co.selectedQuestion.qType')

I think this error is because selectedQuestion is undefined, but I don't know how to set this property. Has anyone got a suggestion?

Aucun commentaire:

Enregistrer un commentaire