如何在 angular5 中测试 Observable 的 ngrx store.select?
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%
我想了解测试中缺少什么来覆盖我正在处理的文件,以实现 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%