触发效果后,我想在单元测试中测试两个可观察值,以获取此部分代码的100%代码覆盖率。因为a window.location.href被触发,所以我无法正确测试。
export class RouteHelper {
static redirectToExternalUrl(url: string): any {
window.location.href = url;
}
}
Run Code Online (Sandbox Code Playgroud)
影响
@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']],
}),
);
}
}),
);
Run Code Online (Sandbox Code Playgroud)
测试else条件
it('should dispatch router action Go on success if no redirect url is provided', () => {
const payload = { redirect: …Run Code Online (Sandbox Code Playgroud) 从 Angular 7 升级到 Angular 8 后,应用程序路由的 loadChildren 发生了重大变化。当这些问题得到修复并且所有测试都在运行时,我不再获得 100% 的代码覆盖率,因为 loadChildren 不再是“字符串”,而是 LoadChildrenCallBack。
我如何测试这部分代码
设置:
import { NgModule } from '@angular/core';
import { RouterModule, RouterStateSnapshot, Routes } from '@angular/router';
import {
RouterStateSerializer,
StoreRouterConnectingModule,
} from '@ngrx/router-store';
import { LanguageGuard } from '@routes/guards/language-guard.service';
import { RouterStateUrl } from 'interfaces';
import { ErrorPageComponent } from 'pages';
/**
* Class to implements the RouterStateSerializer with a custom serializer
*/
export class CustomSerializer implements RouterStateSerializer<RouterStateUrl> {
/**
* Serialize the RouterState with the …Run Code Online (Sandbox Code Playgroud)