我正在尝试测试已注入Store的组件。测试没有通过并给我以下错误:“无法读取未定义的属性'ids'”
测试:
const initialState = {
quoteInfo: {
quoteLoaded: true,
ids: [0],
entities: {
'0': {
id: 0,
quoteId: 'string',
quoteStatus: 0,
reference: 'ref01',
}
}
},
quoteSettings: {
quoteSettingsReceived: true,
ids: [1],
entities: {
'1': {
id: 1,
status: 0,
customer: 'string',
customerId: 'cus01',
carrier: 'car01',
contact: 'contact01',
contactId: 'c01',
priceCategory: 'vip',
priceCategoryId: 'pr1',
creationDate: null,
expirationDate: null,
deliveryDate: null,
currency: null,
currencyId: null,
paymentDescription: 'pay desc',
tax: 0,
comments: 'comments',
shippingAddress: null,
billingAddress: null
}
}
}
};
每次测试:
beforeEach( async(() => {
TestBed.configureTestingModule({
declarations: [GeneralComponent],
imports: [
BrowserAnimationsModule,
HttpClientModule,
StoreModule.forRoot({ feature: combineReducers({quoteSettings: settingsReducer, quoteInfo: infoReducer }, initialState)}),
],
providers: [
]
}).compileComponents();
}));
组件初始化中出现错误,代码如下:
ngOnInit() {
this.randomObservable$ = this._store.pipe(select(randomSelector));
}
随机选择器
export const randomFeatureSelector= createFeatureSelector<RandomState>('random');
export const randomSelector = createSelector(
randomFeatureSelector, // this is null
fromRandomReducer.selectAll
);
问题是在html文件中,我直接从选择器使用observable,如果存储仍未初始化,则会出现该错误
对于模拟存储,我通常使用rx.js中的BehaviorSubject:
let mockStore;
beforeEach( async(() => {
mockStore = new BehaviorSubject(initialState);
TestBed.configureTestingModule({
declarations: [GeneralComponent],
imports: [
BrowserAnimationsModule,
HttpClientModule
],
providers: [
{provide: Store, useValue: mockStore}
]
}).compileComponents();
}));
...