Search code examples
angular

Access a private variable in spec.ts, Angular6


I am using a private variable in Angular-6 service.ts.

private tagSubject = new Subject<any>();

It is been used like

  sendNewTagMessage(message: string) {
    this.tagSubject.next({ text: message });
  }

  clearNewTagMessage() {
    this.tagSubject.next();
  }

I want to write a unit test for tagSubject.

I cant do service.tagSubject.subscribe in the spec.ts as it is giving error like Property 'tagSubject' is private and only accessible within class. What can I do now. Please help.


Solution

  • component['tagSubject']

    or

    (component as any).tagSubject