首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NGRX 8:测试调度动作的效果

NGRX 8:测试调度动作的效果
EN

Stack Overflow用户
提问于 2019-10-15 17:07:33
回答 1查看 479关注 0票数 0

我想测试一下效果是否能派送一个动作。从逻辑上讲,下面的测试输出了一个错误,告诉我createActionSuccess不是间谍。问题是我不知道我可以spyOn createOperationSuccess。

代码语言:javascript
复制
describe('[OPERATION] Create Operation', () => {
    it(' should trigger createoperation from operation service', (done) => {
      actions$ = of({type: createOperation.type, operation: CreateOperationDTOMock});
      spyOn(service, 'create').and.callThrough();
      effects.createOperation$.subscribe(t => {
        expect(service.create).toHaveBeenCalledWith(CreateOperationDTOMock);
        expect(createOperationSuccess).toHaveBeenCalledWith({operation_id: OperationMock._id});
        done();
      });
    });
 });

createOperationSuccess操作:

代码语言:javascript
复制
export const createOperationSuccess = createAction(
  '[OPERATION] Create Operation Success',
  props<{ operation_id: string }>()
);

createOperation效果:

代码语言:javascript
复制
createOperation$ = createEffect(() =>
    this.actions$.pipe(
      ofType(createOperation),
      concatMap(payload => {
        return this.operationService
          .create(payload.operation)
          // I want to test that line below saying that createOperationSuccess has been triggered.
          .pipe(map(operation_id => createOperationSuccess({ operation_id })));
      })
    )
);

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-15 17:39:26

代码语言:javascript
复制
// create an actions stream and immediately dispatch a GET action
actions$ = of({ type: 'GET CUSTOMERS' });

// mock the service to prevent an HTTP request
customersServiceSpy.getAllCustomers.and.returnValue(of([...]));

// subscribe to the Effect stream and verify it dispatches a SUCCESS action
effects.getAll$.subscribe(action => {
  expect(action).toEqual({
    type: 'GET CUSTOMERS SUCCESS',
    customers: [...],
  });
});

https://ngrx.io/guide/effects/testing

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

https://stackoverflow.com/questions/58391050

复制
相关文章

相似问题

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