Unit testing angular 5 component with @ViewChild : <p>I am using angular 5.2.0. I have a child component</p>

<pre><code>import { Component } from '@angular/core';

@Component({
    template: `&lt;div&gt;&lt;\div&gt;`
})
export class ChildComponent {

    public childMethod() {
        ...
    }
}
</code></pre>

<p>and a parent component which accesses the child via <code>ViewChild</code></p>

<pre><code>import { Component, ViewChild } from '@angular/core';
import { ChildComponent } from 'child.component';

@Component({
    template: `&lt;child-component #child&gt;&lt;\child-component&gt;`
})
export class ParentComponent {

    @ViewChild('child')
    public child: ChildComponent;

    public parentMethod() {
        this.child.childMethod();
    }
}
</code></pre>

<p>I want a unit test proving that an invocation of <code>parentMethod</code> causes an invocation of <code>childMethod</code>. I have the following:</p>

<pre><code>import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ChildComponent } from './child.component';
import { ParentComponent } from './parent.component';

describe('ParentComponent', () =&gt; {

    let component: Parentcomponent;
    let fixture: ComponentFixture&lt;Parentcomponent&gt;;

    beforeEach(() =&gt; {
        TestBed.configureTestingModule({
            declarations: [ ParentComponent, ChildComponent ],
            schemas: [ NO_ERRORS_SCHEMA ]
    }).compileComponents();
});

beforeEach(() =&gt; {
    fixture = TestBed.createComponent(TaskListPaginatorComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
});

    it('should invoke childMethod when parentMethod is invoked', () =&gt; {
        const childMethodSpy: jasmine.Spy = spyOn(component.child, 'childMethod');
        component.parentMethod();
        expect(childMethodSpy).toHaveBeenCalled();
    });

});
</code></pre>

<p>Yet, this does not work, and I get <code>Error: &lt;spyOn&gt; : could not find an object to spy upon for childMethod()</code>. </p>

<p>Moreover, this is not a unit test, because I use the real ChildComponent instead of a mock. I tried creating a MockChildComponent and adding it to <code>declarations</code> and <code>export</code> but I got the same result. Any help?</p>

<p>I know there are similar post, but they are for different versions of angular, and they did not help. </p>
