Привет. Я пишу тестовый блок. Я использую Jasmine для своего компонента Angular 5.При загрузке страницы я вызываю метод loadscopesdata для заполнения сетки в моем HTML-коде.Ниже приведен мой пример модульного теста.
it('Ng on init', () => {
component.ngOnInit();
});
Ниже приведен мой действительный код компонента.
ngOnInit() {
this.loadScopesData();
}
Ниже приведена моя функция loadscopedata.
loadScopesData() {
this.scopeService.getScopesByTenantId(this._searchText, this._onlyActive).subscribe(results => this.onScopesDataLoadSuccessful(results), error => this.onScopesDataLoadFailed(error));
}
Ниже приведена мояФункция onScopesDataLoadFailed.
onScopesDataLoadFailed(error: any) {
if (typeof error.error.title != 'undefined') {
this.alertService.stopLoadingMessage();
this.alertService.showMessage(error.error.title, error.error.status, MessageSeverity.error);
this.rows = [];
this.loadingIndicator = false;
}
}
Я могу внедрить свои фиктивные службы и получить некоторые фиктивные данные, все работает, как и ожидалось, но я получаю ошибку ниже
TypeError: Cannot read property 'title' of undefined
at ScopeEditorComponent.onScopesDataLoadFailed (http://localhost:9876/base/boot-tests.ts?195796ce0b6971c676ffa17a1f170941fa665d9e:135378:32)
Ниже приведен мой обновленный код..
it('Ng on init', () => {
component.ngOnInit();
it('should error', () => {
spyOn(component['ScopeEndpoint'], 'getScopesByTenantId').and.callFake(() => Observable.throw({
error: {
title: 'Mocked title',
status: 404 // Mocked status
}
}));
});
});
Может кто-нибудь помочь мне, как обработать метод onScopesDataLoadFailed и избавиться от этой ошибки?Любая помощь будет принята с благодарностью.