首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >测试效果NgRx Angular 11

测试效果NgRx Angular 11
EN

Stack Overflow用户
提问于 2021-02-20 00:18:35
回答 1查看 596关注 0票数 0

我在我的Angular 11项目中测试效果时遇到了问题。我用的是因果报应。似乎当我在我的测试中调度一个动作时,效果似乎没有响应。

代码语言:javascript
运行
复制
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();
    });
  });
});

我已经尝试了很多方法来进入我的效果的订阅部分(使用弹珠热冷...),但它似乎不起作用。我有一个失败,表明:

代码语言:javascript
运行
复制
"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.com/Ushelajyan/simple-ngrx-testing-effects

提前感谢您的帮助。

EN

回答 1

Stack Overflow用户

发布于 2021-02-20 00:27:54

您可以在测试(it-block)中覆盖actions$。不幸的是,TestBed.configureTestingModule()beforeEach块中执行,这发生在it-block执行之前。

代码语言:javascript
运行
复制
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();
    });
  });
});

这应该能起到作用。我添加了一个createEffect函数,它使用给定的actions$模拟正确地配置您的TestBed,并返回一个新的SimpleEffects实例。然后,可以使用此实例订阅其定义的效果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66281222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档