Angular 发送时测试@Effect:false

Angular 发送时测试@Effect:false,angular,redux,rxjs,ngrx,Angular,Redux,Rxjs,Ngrx,我试图测试@Effect({dispatch:false})是否正在调用trackEvent()方法,但该效果不会引发任何进一步的操作,因此无法订阅可观察到的效果 我的效果如下: @Effect({ dispatch: false }) public trackPageView$: Observable<any> = this.action$ .ofType(ActionTypes.TRACK_PAGE_VIEW) .map((action: TrackPageViewAct

我试图测试
@Effect({dispatch:false})
是否正在调用
trackEvent()
方法,但该效果不会引发任何进一步的操作,因此无法订阅可观察到的效果

我的效果如下:

@Effect({ dispatch: false })
public trackPageView$: Observable<any> = this.action$
  .ofType(ActionTypes.TRACK_PAGE_VIEW)
  .map((action: TrackPageViewAction) => {
     this.angulartics2Service.trackPageView(action.payload.path);
  });
it(`should call #trackPageView('${testPath}')`, () => {
  runner.queue(trackPageView(testPath));
  let s = TestBed.get(Angulartics2Service);
  expect(s.trackPageView.called).toBeTruthy(); // <-- fails!
});
let angulartics2Service: Stubbed<Angulartics2Service>;

angulartics2Service = {
  trackPageView: sinon.stub()
}
let actions: Observable<any>;
TestBed.configureTestingModule({
  imports: [
    StoreModule.forRoot(reducers)
  ],
  providers: [
    provideMockActions(() => actions),
    MyEffects  // whatever your effects class is
  ]
})
const effects: MyEffects = TestBed.get(MyEffects);
…在哪里

export type Stubbed<T> = {
  [key in keyof T]?: T[key] & SinonStub;
};
导出类型存根={
[key in keyof T]?:T[key]&SinonSub;
};
调用
trackPageView()
方法时如何测试此效果


感谢您的帮助。

订阅observable应该可以工作,但您仍然需要通过适当的操作触发:

it(`should call #trackPageView('${testPath}')`, () => {
  runner.queue(trackPageView(testPath));
  let s = TestBed.get(Angulartics2Service);

  // set up whatever action is supposed to trigger the effect
  const sourceAction = new TrackPageViewAction();

  // set up the action observable to trigger the effect
  actions = hot('a', { a: sourceAction });

  // then subscribe to the effect
  effects.trackPageView$.subscribe(() => {
    expect(s.trackPageView.called).toBeTruthy();
  });
});
这假设您已经设置了效果/模拟动作,看起来像这样:

@Effect({ dispatch: false })
public trackPageView$: Observable<any> = this.action$
  .ofType(ActionTypes.TRACK_PAGE_VIEW)
  .map((action: TrackPageViewAction) => {
     this.angulartics2Service.trackPageView(action.payload.path);
  });
it(`should call #trackPageView('${testPath}')`, () => {
  runner.queue(trackPageView(testPath));
  let s = TestBed.get(Angulartics2Service);
  expect(s.trackPageView.called).toBeTruthy(); // <-- fails!
});
let angulartics2Service: Stubbed<Angulartics2Service>;

angulartics2Service = {
  trackPageView: sinon.stub()
}
let actions: Observable<any>;
TestBed.configureTestingModule({
  imports: [
    StoreModule.forRoot(reducers)
  ],
  providers: [
    provideMockActions(() => actions),
    MyEffects  // whatever your effects class is
  ]
})
const effects: MyEffects = TestBed.get(MyEffects);
let动作:可观察;
TestBed.configureTestingModule({
进口:[
StoreModule.forRoot(减速器)
],
供应商:[
提供打包操作(()=>操作),
MyEffects//无论您的效果类是什么
]
})
常量效果:MyEffects=TestBed.get(MyEffects);

讨论这个问题有点晚了,但我遇到了同样的问题,最后来到了这里。假设我正在测试以下效果:

  @Effect()
  onPlaceOrder$ : Observable<any> = this.actions$.pipe(
    ofType(CheckoutActionTypes.PlaceOrderAction),
    map(() => new PlaceOrderSuccess())
  );

  @Effect({ dispatch: false })
  onPlaceOrderSuccess$ : Observable<any> = this.actions$.pipe(
    ofType(CheckoutActionTypes.PlaceOrderSuccessAction),
    tap(() => {
      this.router.navigateByUrl('/checkout/thank-you');
    })
  );
顺便说一下,我用茉莉花大理石做测试:


我需要通过调用第一个expect块(
expect(effects.onPlaceOrderSuccess$).toBeObservable(expected);
)来触发
onPlaceOrderSuccess
效果,以便运行效果。然后发现
dispatch:false
效果的返回值实际上是原始操作。这就是如何测试调用
trackPageView()
方法的方法。

也有同样的问题,非常感谢。这是我让它运行的唯一方法。不幸的是,在测试
dispatch:false
effects时有很多错误信息。