測試效果 NgRx Angular 11 (Testing effects NgRx Angular 11)


問題描述

測試效果 NgRx Angular 11 (Testing effects NgRx Angular 11)

我在嘗試在 Angular 11 項目中測試效果時遇到問題。我使用業力賽跑者。似乎當我在測試中調度一個動作時,效果似乎沒有響應它。

import { TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { provideMockActions } from '@ngrx/effects/testing';
import {
  SimpleActionTypes,
} from '../actions/simple.action';
import {SimpleEffects} from './simple.effect';
import {MockStore, provideMockStore} from '@ngrx/store/testing';
import {Store} from '@ngrx/store';

describe('SimpleEffects', () => {
  let actions$: any;
  let effects: SimpleEffects;
  let store: MockStore<Store>;
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        SimpleEffects,
        provideMockActions(() => actions$),
        provideMockStore() ,
      ],
    });
    store = TestBed.inject(MockStore);
    effects = TestBed.inject(SimpleEffects);
  });
  it('should be created', () => {
    expect(effects).toBeTruthy();
  });
  it('should fire with a default price', (done) => {
    // Mock the action that we use to activate the effect
    actions$ = of(SimpleActionTypes.UnavailablePrice);
    // Subscribe to the effect
    effects.getPriceAfterLocalCartUpdate.subscribe((res) => {
      // the expected results verification
      expect(res).toEqual(SimpleActionTypes.ComputePrice);
      // And all done !
      done();
    });
  });
});

我試過很多方法進入我效果的訂閱部分(使用彈珠冷熱......),但它似乎不起作用。我有一個失敗的指示:

"SimpleEffects should fire with a default price FAILED
        Error: Timeout ‑ Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)"

微型項目託管在這裡:https://github。


參考解法

方法 1:

You override actions$ within your test (it‑block). Unfortunately the TestBed.configureTestingModule() gets executed inside a beforeEach block, which happens right before the it‑block gets executed.

import { TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { provideMockActions } from '@ngrx/effects/testing';
import {
  SimpleActionTypes,
} from '../actions/simple.action';
import {SimpleEffects} from './simple.effect';
import {MockStore, provideMockStore} from '@ngrx/store/testing';
import {Store} from '@ngrx/store';

describe('SimpleEffects', () => {
  let store: MockStore<Store>;

  const createEffects = (actions$) => {
    TestBed.configureTestingModule({
      providers: [
        SimpleEffects,
        provideMockActions(() => actions$),
        provideMockStore() ,
      ],
    });
    store = TestBed.inject(MockStore);
    return TestBed.inject(SimpleEffects);
  };

  it('should be created', () => {
    const effects = createEffects(of(undefined));

    expect(effects).toBeTruthy();
  });

  it('should fire with a default price', (done) => {
    // Mock the action that we use to activate the effect
    const actions$ = of(SimpleActionTypes.UnavailablePrice);

    // Create the effect with your given mock
    const effects = createEffects(actions$)

    effects.getPriceAfterLocalCartUpdate.subscribe((res) => {
      // the expected results verification
      expect(res).toEqual(SimpleActionTypes.ComputePrice);
      // And all done !
      done();
    });
  });
});

This should do the trick. I added a createEffect function that configures your TestBed properly with the given actions$ mock and returns a new instance of SimpleEffects. This instance can then be used to subscribe to its defined effects.

(by UshelajyanPhilipp Meissner)

參考文件

  1. Testing effects NgRx Angular 11 (CC BY‑SA 2.5/3.0/4.0)

#javascript #ngrx-effects #ngrx #Angular #karma-jasmine






相關問題

為什麼我不能在 IE8 (javascript) 上擴展 localStorage? (Why can't I extend localStorage on IE8 (javascript)?)

在 Javascript 中打開外部 sqlite3 數據庫 (Open external sqlite3 database in Javascript)

Javascript:數組中的所有對像都具有相同的屬性 (Javascript: All Objects in Array Have Same Properties)

為什麼我們要在 javascripts 原型中添加函數? (Why do we add functions to javascripts prototype?)

顯示 URL javascript 的最後一部分? (Display the last part of URL javascript?)

Javascript XMLHttpRequest:忽略無效的 SSL 證書 (Javascript XMLHttpRequest: Ignore invalid SSL Certificate)

有沒有辦法測試 console.log 整體 (Is there a way to test for console.log entires)

如何從 javascript 對像中獲取名稱和值到新列表中? (How to get name and values from a javascript object into a new list?)

數據未發布..幫助!html,js,firebase (Data not posting.. Help! html,js,firebase)

使用 Node.js 腳本查看表單數據 (Seeing form data with Node.js script)

使用百分比查找範圍內的值 (find the value within a range using percent)

如何通過 react.js 中的組件傳遞變量或數據? (How to pass varible or data through components in react.js?)







留言討論