NGXS:如何测试是否已分派动作? [英] NGXS: How to test if an action was dispatched?

查看:99
本文介绍了NGXS:如何测试是否已分派动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对是否已分派动作进行单元测试?

例如,在LogoutService中,我有以下简单方法:

 注销(用户名:字符串){store.dispatch([新的ResetStateAction(),新的LogoutAction(用户名)]);} 

我需要编写一个单元测试,以验证是否已分派了两个操作:

 它(应调度ResetState和Logout操作",功能(){logoutService.logout();//如何检查分派的动作及其参数?//预计(...)}); 

如何检查已调度的动作?

解决方案

NGXS管道运算符

NGXS中的动作由Observables处理.NGXS为您提供了管道运算符,为了进行测试,您可以使用 ofActionDispatched .这是我从 NGXS文档

中获取的列表:

  • ofAction 在以下任何生命周期事件发生时触发
  • ofActionDispatched 在已分派动作时触发
  • ofActionSuccessful 在动作完成时触发成功
  • ofActionCanceled 在取消动作时触发
  • ofActionErrored 在某个动作导致错误被触发时触发抛出
  • ofActionCompleted 在动作完成时触发是否成功(返回完成摘要)

答案

1.创建变量 actions $

  describe('control-center.state',()=> {let actions $:可观察的< any> ;;//...}); 

2.用可观察的

初始化变量 actions $

  beforeEach(()=> {TestBed.configureTestingModule({进口:[NgxsModule.forRoot([AppState]),NgxsModule.forFeature([ControlCenterState])]});商店= TestBed.get(商店);actions $ = TestBed.get(Actions);}) 

3.1测试是否已调用1个操作:

使用操作符 ofActionsDispatched()从流中过滤您的操作.

  it('应该分派LogoutAction',(完成)=> {actions $ .pipe(ofActionDispatched(LogoutAction)).subscribe((_)=> {完毕();});service.logout();}); 

3.2测试是否已调用多个动作:

使用RXJS zip运算符将两个可观察对象与 ofActionsDispatched()函数结合使用(zip:在所有可观察对象发射之后,将值作为数组发射).

  it('应该分派ResetStateAction和LogoutAction',(完成)=> {压缩(actions $ .pipe(ofActionDispatched(ResetStateAction)),actions $ .pipe(ofActionDispatched(LogoutAction))).subscribe((_)=> {完毕();});service.logout();}); 


该规范只有在其 done 被调用后才能完成.如果未调用 done ,则会引发超时异常.

来自茉莉花文档.

How to unit test whether an action was dispatched?

For example, in a LogoutService, I have this simple method:

  logout(username: string) {
    store.dispatch([new ResetStateAction(), new LogoutAction(username)]);
  }

I need to write a unit test that verifies that the two actions are dispatched:

  it('should dispatch ResetState and Logout actions', function () {
    logoutService.logout();

    // how to check the dispactched actions and their parameters?
    // expect(...)
  });

How can I check the dispatched actions?

解决方案

NGXS Pipeable Operators

Actions in NGXS are handled with Observables. NGXS provides you Pipeable Operators, for your test you could use the ofActionDispatched. Here is the list I have taken from the NGXS documentation:

  • ofAction triggers when any of the below lifecycle events happen
  • ofActionDispatched triggers when an action has been dispatched
  • ofActionSuccessful triggers when an action has been completed successfully
  • ofActionCanceled triggers when an action has been canceled
  • ofActionErrored triggers when an action has caused an error to be thrown
  • ofActionCompleted triggers when an action has been completed whether it was successful or not (returns completion summary)

Answer

1. Create variable actions$

describe('control-center.state', () => {
  let actions$: Observable<any>;

  // ...
});

2. Initialize variable actions$ with observable

beforeEach(() => {
  TestBed.configureTestingModule({
    imports: [
      NgxsModule.forRoot([AppState]),
      NgxsModule.forFeature([ControlCenterState])
    ]
  });
  store = TestBed.get(Store);
  actions$ = TestBed.get(Actions);
})

3.1 Test if 1 action has been called:

Filter your actions from the stream with the operator ofActionsDispatched().

it('should dispatch LogoutAction', (done) => {
  actions$.pipe(ofActionDispatched(LogoutAction)).subscribe((_) => {
    done();
  });

  service.logout();
});

3.2 Test if multiple actions have been called:

Use the RXJS zip operator to combine the two observables with the ofActionsDispatched() function (zip: after all observables emit, emit values as an array).

it('should dispatch ResetStateAction and LogoutAction', (done) => {
  zip(
    actions$.pipe(ofActionDispatched(ResetStateAction)),
    actions$.pipe(ofActionDispatched(LogoutAction))
  ).subscribe((_) => {
    done();
  });

  service.logout();
});


The spec will not complete until its done is called. If done is not called a timeout exception will be thrown.

From the Jasmine documentation.

这篇关于NGXS:如何测试是否已分派动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆