我正在尝试测试@Effect({ dispatch: false })是否正在调用trackEvent()方法,但该效果不会引发任何进一步的操作,因此不可能订阅可观察到的described in the docs效果。
我的效果看起来像这样:
@Effect({ dispatch: false })
public trackPageView$: Observable<any> = this.action$
.ofType(ActionTypes.TRACK_PAGE_VIEW)
.map((action: TrackPageViewAction) => {
this.angulartics2Service.trackPageView(action.payload.path);
});...and我的规范的摘录如下所示:
it(`should call #trackPageView('${testPath}')`, () => {
runner.queue(trackPageView(testPath));
let s = TestBed.get(Angulartics2Service);
expect(s.trackPageView.called).toBeTruthy(); // <-- fails!
});我的angulartics2Service看起来像这样:
let angulartics2Service: Stubbed<Angulartics2Service>;
angulartics2Service = {
trackPageView: sinon.stub()
}...where
export type Stubbed<T> = {
[key in keyof T]?: T[key] & SinonStub;
};如何测试调用trackPageView()方法的效果?
感谢您的帮助。
发布于 2019-01-16 22:38:22
来得有点晚,但我遇到了同样的问题,并最终来到了这里。假设我正在测试以下效果:
@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');
})
);这就是我最终测试它们的方式:
describe('when PlaceOrderAction is triggered', () => {
let expected;
const placeOrderAction = new PlaceOrder();
beforeEach(() => {
const placeOrderSuccessAction = new PlaceOrderSuccess();
actions$ = hot('--a', { a: placeOrderAction });
expected = cold('--b', { b: placeOrderSuccessAction });
});
it('should dispatch a PlaceOrderSuccess action', () => {
expect(effects.onPlaceOrder$).toBeObservable(expected);
});
});
describe('when PlaceOrderSuccessAction is triggered', () => {
let expected;
const placeOrderSuccessAction = new PlaceOrderSuccess();
beforeEach(() => {
actions$ = hot('--a', { a: placeOrderSuccessAction });
expected = cold('--b', { b: placeOrderSuccessAction });
});
it('should call router.navigateByUrl', () => {
//the actual effect doesn't trigger unless the following expect is run
expect(effects.onPlaceOrderSuccess$).toBeObservable(expected);
expect(router.navigateByUrl).toHaveBeenCalledWith('/checkout/thank-you');
});
});顺便说一下,我正在使用茉莉花大理石进行测试:https://github.com/synapse-wireless-labs/jasmine-marbles
为了让效果运行,我需要通过调用第一个expect块(expect(effects.onPlaceOrderSuccess$).toBeObservable(expected);)来触发onPlaceOrderSuccess效果。然后,事实证明,dispatch: false效果的返回值实际上是原始动作。这就是测试trackPageView()方法是否被调用的方法。
发布于 2018-05-24 21:06:09
订阅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();
});
});这里假设您已经设置了effects/mock操作,如下所示:
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);https://stackoverflow.com/questions/46080070
复制相似问题