簡體   English   中英

如何單元測試從 angular 10 中的“@angular-redux/store”中選擇?

[英]how to unit test select from "@angular-redux/store" in angular 10?

我有一個減速器定義為:

export const dummy_reducer = (state: any = INITIAL_STATE, action: any): any => {


  const actionOptions = (actionType) => ({
    GET_EMPLOYEE_DETAILS: () => {
      return tassign(state, {
        emp_name: action.payload.emp_name
      });
    },

我在我的 component.ts 中使用它,如下所示:

 @select(["dummy_reducer", "emp_name"])
  emp_name$: Observable<any>;

此外,當組件初始化時,我使用 emp_name$ 進行訂閱,如下所示:

ngOnInit() { this.emp_name$.subscribe((details) => { ......... } }); }

現在我的問題是如何為這個特定組件編寫單元測試,當我運行 ng test 時,我收到錯誤:

 Cannot read property 'subscribe' of undefined ...

在這個 ngOnInIt()。 關於如何修復它的任何建議? 請讓我知道如何為此編寫成功的規范文件。

您需要導入NgReduxTestingModule ,然后在組件內部使用@select('loading') loading$: Observable<boolean>我們可以對其進行測試:

const stub = MockNgRedux.getSelectorStub('loading');
stub.next(false);
stub.next(true);
stub.complete();

component.loading$
  .toArray()
  .subscribe(
    actualSequence => expect(actualSequence).toEqual([ false, true ]),
    null,
    done);

您甚至可以模擬beforeEach函數中的數據,以便它可用於所有測試:)

這里獲取的代碼參考檢查它以獲取更多測試示例

暫無
暫無

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

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