title: Jasmine test angular component with private property
body: <p>I have this code. I save <code>myData</code> in <code>ngOnInit</code>. When I test for myMethod on the click of a button this.myData is <code>undefined</code> in myMethod.</p>
  
  <pre><code>@Component({
     templateUrl: ''
  })
  
  export class MyComponent implements OnInit {
     myData: MyClass;
     constructor() {}
     ngOnInit() {
        this.myData = // some code
     }
  
     myMethod() {
      this.myData is undefined here
     }
  }
  </code></pre>
  
  <p>My spec file is:</p>
  
  <pre><code>describe('MyComponent', () =&gt; {
  
   // set up stubs 
   beforeEach(async(() =&gt; {
   TestBed.configureTestingModule({
    declarations: [  MyComponent ],
    providers:    [
    ],
    imports: [
     FormContainer
    ],
    schemas: [NO_ERRORS_SCHEMA],
  })
  .compileComponents();
  
    fixture = TestBed.createComponent(MyComponent);
    comp = fixture.debugElement.componentInstance;
   }));
  
    it('should call myMethod', fakeAsync(() =&gt; {
      element.querySelectorAll('button')[0].click();
      fixture.detectChanges();
      fixture.whenStable().then(async() =&gt; {
        fixture.detectChanges();
      });
    }));
  });
  </code></pre>