簡體   English   中英

Angular 6 ngrx存儲測試:無法讀取未定義的ID

[英]Angular 6 ngrx store testing: cannot read ids of undefined

我正在嘗試測試已注入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();

}));

...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM