Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 单元测试NGRX效果与外部URL的大理石_Angular_Ngrx_Ngrx Effects_Jasmine Marbles - Fatal编程技术网

Angular 单元测试NGRX效果与外部URL的大理石

Angular 单元测试NGRX效果与外部URL的大理石,angular,ngrx,ngrx-effects,jasmine-marbles,Angular,Ngrx,Ngrx Effects,Jasmine Marbles,当触发一个效果时,我想在单元测试中测试这两个观察值,以获得这部分代码的100%代码覆盖率。由于触发了window.location.href,我无法正确测试它 export class RouteHelper { static redirectToExternalUrl(url: string): any { window.location.href = url; } } 影响 @Effect() handleCreatePaymentSuccess$: Obs

当触发一个效果时,我想在单元测试中测试这两个观察值,以获得这部分代码的100%代码覆盖率。由于触发了
window.location.href
,我无法正确测试它

export class RouteHelper {
    static redirectToExternalUrl(url: string): any {
        window.location.href = url;
    }
}
影响

@Effect()
handleCreatePaymentSuccess$: Observable<
    {} | routerActions.Go
> = this.actions$.pipe(
    ofType(cartConfigActions.CREATE_PAYMENT_SUCCESS),
    switchMap((action: any) => {
        if (action.payload.redirect) {
            /* istanbul ignore next */
            return Observable.create(
                RouteHelper.redirectToExternalUrl(action.payload.redirect),
            );
        } else {
            return of(
                new routerActions.Go({
                    path: [RouteHelper.paths['validate']],
                }),
            );
        }
    }),
);
测试在if条件下不工作

it('should dispatch router action Go on success if no redirect url is provided', () => {
    const payload = { redirect: null };
    const action = new fromCartConfig.CreatePaymentSuccess(payload);
    const completion = new routeractions.Go({
        path: [RouteHelper.paths['validate']],
    });

    actions$.stream = cold('-a', { a: action });
    const expected = cold('-c', { c: completion });

    expect(effects.handleCreatePaymentSuccess$).toBeObservable(expected);
});
it('should redirect to url that is returned from api', () => {
    const payload = { redirect: 'http://www.stackoverflow.com' };
    spyOn(RouteHelper, 'redirectToExternalUrl').withArgs(payload.redirect);

    const action = new fromCartConfig.CreatePaymentSuccess(payload);
    const completion = Observable.create(RouteHelper.redirectToExternalUrl);

    actions$.stream = cold('-a', { a: action });
    const expected = cold('-c', { c: completion });

    expect(effects.handleCreatePaymentSuccess$).toBeObservable(expected);
});

有人能解释一下如何测试If条件吗?

您可以使
RouteHelper
可注入,并在测试期间提供模拟实现。这样,您就可以验证该方法是否已被调用。

解决方案:

it('should dispatch router action Go on success if redirect url is provided', () => {
    spyOn(RouteHelper, 'redirectToExternalUrl').and.callFake(() => {});
    const payload = { redirect: 'www.buckaroo.nl' };
    const action = new fromCartConfig.CreatePaymentSuccess(payload);

    actions$.stream = cold('-a', { a: action });
    const expected = cold('');

    expect(effects.handleCreatePaymentSuccess$).toBeObservable(expected);
    expect(RouteHelper.redirectToExternalUrl).toHaveBeenCalled();
});