Учитывая следующий код, я пытаюсь проверить крючок ngOnChanges
жизненного цикла Angular2:
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: `<p>{{value}}</p>`,
})
export class TestComponent implements OnChanges {
@Input() value: string;
ngOnChanges(changes: {}): any {
// should be called
}
}
fdescribe('TestComponent', () => {
let tcb: TestComponentBuilder;
beforeEachProviders(() => [
TestComponentBuilder,
TestComponent,
]);
beforeEach(inject([TestComponentBuilder], _tcb => {
tcb = _tcb;
}));
it('should call ngOnChanges', done => {
tcb.createAsync(TestComponent).then(fixture => {
let testComponent: TestComponent = fixture.componentInstance;
spyOn(testComponent, 'ngOnChanges').and.callThrough();
testComponent.value = 'Test';
fixture.detectChanges();
expect(testComponent.ngOnChanges).toHaveBeenCalled();
done();
}).catch(e => done.fail(e));
});
});
К сожалению, тест завершился неудачно с сообщением Expected spy ngOnChanges to have been called.
Я знаю, что я мог бы просто проверить содержимое HTML-элемента в этом примере, но у меня есть код, который необходимо протестировать внутри крючка жизненного цикла ngOnChanes, так что решение для меня. Я также не хочу напрямую вызывать testComponent.ngOnChanges({someMockData});
в тесте.
Как я могу установить TestComponent.value
из теста, чтобы вызвать ngOnChanges
?