title: How to test Component having a constructor with child component object in it - in Angular2 (Jasmine -Karma)
body: <p>I am writing a test cases using Jasmine Karma in Angular 2 component which having constructor. In this constructor I am passing other component object as a parameter. When trying to access methods of other component are getting undefined.</p>
  
  <p><strong>1. TestComponent</strong></p>
  
  <pre><code>@Component({
      template: `&lt;div&gt;&lt;/div&gt;`
  })
  class TestComponent implements OnInit {
      constructor(public otherComponent: OtherComponent) {}
      ngOnInit() {}
      callMyFunction() {
          this.otherComponent.otherFunction();
      }
  }
  </code></pre>
  
  <p><strong>2. OtherComponent</strong> </p>
  
  <pre><code>@Component({
      selector: "other-component",
      template: `&lt;div&gt;&lt;/div&gt;`
  })
  export class OtherComponent implements OnInit {
      ngOnInit() {}
      public otherFunction(): void {
          console.log('test function called');
      }
  }
  </code></pre>
  
  <p><strong>3. Test Case</strong></p>
  
  <pre><code>describe("TestComponent", () =&gt; {
  
      let comp: TestComponent;
      let fixture: ComponentFixture &lt; TestComponent &gt; ;
      let de: DebugElement;
  
      beforeEach(async(() =&gt; {
          TestBed.configureTestingModule({
              imports: [FormsModule],
              declarations: [TestComponent, OtherComponent],
          }).compileComponents();
      }));
  
      beforeEach(() =&gt; {
          fixture = TestBed.createComponent(TestComponent);
          comp = fixture.componentInstance;
          de = fixture.debugElement;
          fixture.detectChanges();
      });
  
      fit("Should call otherFunction of OtherComponent.", () =&gt; {
          spyOn(comp.otherComponent, "otherFunction").and.callThrough();
          comp.callMyFunction();
          expect(comp.otherComponent.otherFunction).toHaveBeenCalled();
      });
  });
  </code></pre>
  
  <p>Please help me on this.</p>