【问题标题】:Ngrx unit test effectsNgrx 单元测试效果
【发布时间】:2021-06-22 12:18:12
【问题描述】:

我在单元测试 ngrx 效果上有奇怪的错误,这是我的例子

export const noop = (): any => null;
export const obsNoop = () => of({});
export const mockObs = () => {
  return {
    subscribe: () => { }
  };
};

const initialState: Tutorial[] = [
  { id: 1, name: 'Google', url: 'google.com' },
  { id: 2, name: 'Yahoo', url: 'yahoo.com' },
];

class MockTutorialsService {
  getAll = obsNoop;
}

fdescribe('AppEffects', () => {
  let actions$: Observable<any>;
  let effects: TutorialEffects;
  let store: MockStore<AppState>;
  let httpService: TutorialsService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        TutorialEffects,
        provideMockActions(() => actions$),
        provideMockStore({ initialState }),
        { provide: TutorialsService, useClass: MockTutorialsService }
      ],
    });

    effects = TestBed.inject(TutorialEffects);
    store = TestBed.inject(MockStore);
    httpService = TestBed.inject(TutorialsService);
  });

  it('should be created', () => {
    expect(effects).toBeTruthy();
  });

  // Testing API interaction
  describe('onFetchUsers$', () => {
    it('should fire if users is null', (done) => {
      const spy = spyOn(httpService, 'getAll').and.callThrough();
      actions$ = of(TutorialActions.GET_TUTORIALS);
      effects.loadTutorials$.subscribe((res) => {
        expect(res).toEqual(new TutorialActions.SuccesGetTutorials(res));
        expect(spy).toHaveBeenCalledTimes(1);
        done();
      });
    });
  });
});

我得到的错误与业力likeError有关: 超时 - 异步函数未在 5000 毫秒内完成(由 jasmine.DEFAULT_TIMEOUT_INTERVAL 设置)此

有人知道我哪里出错了吗?

这里是特效

Injectable()
export class TutorialEffects {
  loadTutorials$ = createEffect(() =>
    this.action$.pipe(
      ofType(TutorialActions.GET_TUTORIALS),
      switchMap(() =>
        this.tutorialService.getAll().pipe(
          map((tutorial: Tutorial[]) => new TutorialActions.SuccesGetTutorials(tutorial)),
          catchError((error) => of(error))
        )
      )
    )
  );


  constructor(private action$: Actions, private tutorialService: TutorialsService) { }
}

【问题讨论】:

    标签: angular ngrx


    【解决方案1】:

    在没有看到效果的情况下,我在猜测,但问题是测试没有进入 effects.loadTutorials$.subscribe 行内并在 5 seconds 内调用 done

    effects.loadTutorials$.subscribe((res) => {
            console.log('!! Inside of the subscribe !!'); // make sure you see this log
            expect(res).toEqual(new TutorialActions.SuccesGetTutorials(res));
            expect(spy).toHaveBeenCalledTimes(1);
            done();
          });
    

    我认为罪魁祸首是and.callThrough()。我认为你应该返回一个虚假的响应。

    const spy = spyOn(httpService, 'getAll').and.returnValue(of({/* mock response */ });
    

    ====== 编辑 ======

      loadTutorials$ = createEffect(() =>
        this.action$.pipe(
          ofType(TutorialActions.GET_TUTORIALS),
          switchMap(() => {
            console.log('!! Inside of switchMap !!');
            return this.tutorialService.getAll().pipe(
              map((tutorial: Tutorial[]) => { 
               console.log('!! Inside of map !!');
               return new TutorialActions.SuccesGetTutorials(tutorial); }),
              catchError((error) => of(error))
            )
          })
        )
      );
    

    【讨论】:

    • 我添加了效果,console.log 也一无所获
    • 您是否也更改了spy?如果您更改了spy,其他一切似乎对我来说都很好。
    • 是的,我改变了并且窥探了
    • 抱歉,我不知道如何帮助您,只能将这些日志添加到我刚刚进行的编辑中,看看哪个没有记录并确定它们没有记录的原因。
    • 它永远不会订阅,甚至没有一个console.log被触发
    猜你喜欢
    • 1970-01-01
    • 2019-06-22
    • 2018-01-04
    • 1970-01-01
    • 2019-06-23
    • 2021-06-26
    • 2023-03-13
    • 2018-10-05
    • 2022-01-15
    相关资源
    最近更新 更多