gpt4 book ai didi

Angular6 ngrx : Cannot read property 'ids' of undefined

转载 作者:行者123 更新时间:2023-12-02 00:57:47 24 4
gpt4 key购买 nike

我正在尝试做一个简单的选择器测试。但是,测试未通过并给我以下错误:“无法读取未定义的属性‘ids’”。在应用程序中工作正常,但当 selectCustomers被调用,它返回 undefined。

测试

fdescribe('Customer Selector', () => {

const customerState: fromQuoteCustomers.CustomerState = {
customersLoaded: true,
entities: {
'cus01': {
id: 'cus01',
web: 'web01',
cif: 'cif01',
currencyId: 'id-01',
telephone: 666616666,
name: 'name1',
email: 'example@mail.es'
},
'cus02': {
id: 'cus02',
web: 'web02',
cif: 'cif02',
currencyId: 'id-02',
telephone: 111000000,
name: 'name2',
email: 'mail@example.es'
}
},
ids: ['cus01', 'cus02']
};
it('select customers', () => {
expect(selectCustomers(customerState).length).toBe(2);
});
});

选择器代码如下:
export const selectCustomer = createFeatureSelector<CustomerState>('customer');

export const selectCustomers = createSelector(
selectCustomer,
fromCustomer.selectAll
);

reducer :
export const adapter: EntityAdapter<Customer> = createEntityAdapter<Customer>();

export const initialState: CustomerState = adapter.getInitialState({
customersLoaded: false,
});

export function reducer(state = initialState, action: CustomerActions): CustomerState {
if (action) {
switch (action.type) {
case CustomerActionTypes.LoadCustomers:
return adapter.addMany(action.payload.customerList, state);
default:
return state;
}
}
return state;
}

export const {
selectAll,
selectEntities,
selectIds,
selectTotal

} = adapter.getSelectors();

最佳答案

您必须将您的状态包装在根状态中,因为您还使用了 selectCustomer引擎盖下的选择器。

const customerState = {
customer: {
customersLoaded: true,
entities: {
'cus01': {
id: 'cus01',
web: 'web01',
cif: 'cif01',
currencyId: 'id-01',
telephone: 666616666,
name: 'name1',
email: 'example@mail.es'
},
'cus02': {
id: 'cus02',
web: 'web02',
cif: 'cif02',
currencyId: 'id-02',
telephone: 111000000,
name: 'name2',
email: 'mail@example.es'
}
},
ids: ['cus01', 'cus02']
}
};
另一种选择是使用 projector功能如 the docs 中所述.
更多示例可以在 How I test my NgRx selectors 中找到

关于Angular6 ngrx : Cannot read property 'ids' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53087555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com