angular - Angular 6 ngrx 商店测试:无法读取未定义的 id
问题描述
我正在尝试测试一个注入了 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,并且如果商店仍未初始化,则会出现该错误
推荐阅读
- javascript - Zapier 代码:将字符串转换为数组 javascript
- sql - 使用 SQL 为帐户设置生成唯一的访问密钥
- xamarin - 我们如何将 Grids Width 绑定到 xamarin 表单中通用视图模型中的属性列表项?
- java - 如何将 a.jar 转换为另一个自定义扩展?
- authentication - 如何在 Keycloak 中限制 SAML 客户端的某些组的用户身份验证?
- delphi - 线程可以安全地创建 FMX.Graphics.TBitmap.Canvas 吗?
- python - 如何使用 Python3 中的对数属性确保一个数字是另一个数字的幂?
- sql-server - 如何根据具有不同 where 条件和按包含撇号的字段名称分组的日期表最大值获取结果?
- svn - .svnignore 在执行时被 Subversion 忽略:“svn copy”
- javascript - 延迟阅读php代码