首页 > 解决方案 > 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
);

标签: angulartypescriptunit-testingngrx

解决方案


问题出在 html 文件中,如果我直接使用选择器中的 observable,并且如果商店仍未初始化,则会出现该错误


推荐阅读