簡體   English   中英

單元測試Angular組件+ ngrx存儲

[英]Unit testing Angular component + ngrx store

嘗試測試我的角度分量。

我有根狀態和模塊狀態,看起來像:

state {
  auth: {
  // user data
  },
  messagesModule:{
   // here's module state
    messages:[...]
  }
}

初始消息狀態為空對象。

零件:

export class MessagesComponent {
  public messages$: Observable<number>;

  constructor(private store: Store<any>) {
   this.messagesLengh$ = store.pipe(select('getMessagesLengh'));
  }
...

選擇

export const msgLength = (state: AppState) => state.messages.lenght;

測試

describe('MessagesComponent', () => {
  let component: MessagesComponent;
  let fixture: ComponentFixture<MessagesComponent>
  let store: Store<fromFeature.State>

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        StoreModule.forRoot({
          ...fromRoot.reducers,
          messagesModule: combineReducers(fromFeature.reducers),
        }),
    // other imports
      ],
      ...
    });

    store = TestBed.get(Store);

    spyOn(store, 'dispatch').and.callThrough();

    fixture = TestBed.createComponent(MessagesComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should be created', () => {
    expect(component).toBeTruthy();
  });

這就是麻煩所在:測試失敗,因為“找不到未定義的消息”。 我用控制台記錄了我的測試組件,那里有商店。 任何想法,請。

為了測試注入Store的組件,我遵循了此官方文檔。

進行以下操作並刪除CombineReducers行對我有用。

   StoreModule.forRoot({
          ...fromRoot.reducers,
        }),

暫無
暫無

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

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