[英]How to test the ngrx store.select of an Observable, in angular5?
我想了解測試中缺少哪些內容來覆蓋我正在處理的文件,以實現100%的覆蓋率。 以下是需要注意的部分:
給定此打字稿文件:
constructor(
public store: Store<AppState>
) {}
ngOnInit(): void {
this.search$ = this.store.select(s => s.search);
}
而這個測試:
it('should start observing the search store', () => {
const search: any = {
data: {}
};
const state = Observable.of(search);
const store = jasmine.createSpyObj('store', ['select']);
store.select.and.returnValue(state);
component.store = store;
component.ngOnInit();
expect(component.search$).toBe(state);
});
當前的測試范圍:
我想我所缺少的是一個非常愚蠢的東西,我盲目地看不到。 感謝您的提前幫助。
確實如@shaunhusain所建議的,這是該文件的修復程序
ngOnInit(): void {
this.fetch();
this.search$ = this.store.select(this.getStoreSearchData);
}
private getStoreSearchData(store: AppState): Search {
return store.search;
}
及其測試:
describe('#getStoreSearchData', () => {
it('should return the search store', () => {
const search: any = {
data: {}
};
const store = <AppState>{search: search};
const storeSearch = component['getStoreSearchData'](store);
expect(storeSearch).toEqual(search);
});
});
現在覆蓋率已恢復到100%
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.