Testing ngOnChanges lifecycle hook in Angular 2
<p>Given the following code I try to test the <code>ngOnChanges</code> lifecycle hook of Angular2:</p>

<pre class="lang-js prettyprint-override"><code>import {
    it,
    inject,
    fdescribe,
    beforeEachProviders,
} from '@angular/core/testing';

import {TestComponentBuilder} from '@angular/compiler/testing';

import {Component, OnChanges, Input} from '@angular/core';

@Component({
    selector: 'test',
    template: `&lt;p&gt;{{value}}&lt;/p&gt;`,
})
export class TestComponent implements OnChanges {
    @Input() value: string;

    ngOnChanges(changes: {}): any {
        // should be called
    }
}

fdescribe('TestComponent', () =&gt; {
    let tcb: TestComponentBuilder;

    beforeEachProviders(() =&gt; [
        TestComponentBuilder,
        TestComponent,
    ]);

    beforeEach(inject([TestComponentBuilder], _tcb =&gt; {
        tcb = _tcb;
    }));

    it('should call ngOnChanges', done =&gt; {
        tcb.createAsync(TestComponent).then(fixture =&gt; {
            let testComponent: TestComponent = fixture.componentInstance;

            spyOn(testComponent, 'ngOnChanges').and.callThrough();

            testComponent.value = 'Test';
            fixture.detectChanges();

            expect(testComponent.ngOnChanges).toHaveBeenCalled();
            done();
        }).catch(e =&gt; done.fail(e));
    });
});
</code></pre>

<p>Unfortunately the test fails with the message <code>Expected spy ngOnChanges to have been called.</code> I know that I could just check the contents of the HTML Element in this example, but I have some code that needs to be tested inside of the ngOnChanes lifecycle hook, so thats not a solution for me. I also don't want to call <code>testComponent.ngOnChanges({someMockData});</code> in the test directly.</p>

<p>How can I set the <code>TestComponent.value</code> from a test so that <code>ngOnChanges</code> is called?</p>