日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何对SAP Spartacus支持路由的Component进行单元测试

發布時間:2023/12/19 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何对SAP Spartacus支持路由的Component进行单元测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

完整源代碼:

import { Component, NgZone } from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { Router } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; import { RoutingParamsService } from './routing-params.service';@Component({selector: 'cx-mock',template: '', }) export class MockComponent {}describe('RoutingParamsService', () => {let service: RoutingParamsService;let router;let zone: NgZone;beforeEach(() => {TestBed.configureTestingModule({imports: [RouterTestingModule.withRoutes([{path: '',component: MockComponent,},{path: 'budgets',component: MockComponent,children: [{path: ':budgetCode',component: MockComponent,children: [{path: 'children/:childCode',component: MockComponent,},],},],},]),],declarations: [MockComponent],providers: [RoutingParamsService],});service = TestBed.inject(RoutingParamsService);router = TestBed.inject(Router);zone = TestBed.inject(NgZone);});it('should be created', () => {expect(service).toBeTruthy();});it('should not have any params for /budgets', async () => {let result;await zone.run(() => router.navigateByUrl('/budgets'));service.getParams().subscribe((params) => (result = params));expect(result).toEqual({});});it('should have param for /budgets/1234', async () => {let result;await zone.run(() => router.navigateByUrl('/budgets/1234'));service.getParams().subscribe((params) => (result = params));expect(result).toEqual({ budgetCode: '1234' });});it('should have params for /budgets/1234/children/5678', async () => {let result;await zone.run(() => router.navigateByUrl('/budgets/1234/children/5678'));service.getParams().subscribe((params) => (result = params));expect(result.budgetCode).toEqual('1234');expect(result.childCode).toEqual('5678');});it('should share child params', async () => {let result1;let result2;await zone.run(() => router.navigateByUrl('/budgets/1234'));service.getParams().subscribe((params) => (result1 = params));expect(result1.budgetCode).toEqual('1234');await zone.run(() => router.navigateByUrl('/budgets/1234/children/5678'));service.getParams().subscribe((params) => (result2 = params));expect(result1.budgetCode).toEqual('1234');expect(result2.childCode).toEqual('5678');});it('should clear all params', async () => {let result;await zone.run(() => router.navigateByUrl('/budgets/1234'));await zone.run(() => router.navigateByUrl('/budgets'));service.getParams().subscribe((params) => (result = params));expect(result).toEqual({});}); });

首先用RouterTestingModule.withRoutes構造一個支持Route的module出來,其返回作為輸入,傳入TestBed.configureTestingModule的imports數組里:

在單元測試代碼里,使用zone.run啟動一段代碼,調用router.navigateByUrl進行跳轉:

await zone.run(() => router.navigateByUrl('/budgets'));

待await返回后,可以調用subscribe獲取RouteParamsService提取到的路由參數:

如果路由路徑是/budgets/1234,根據之前的路由配置:

提取出的參數為budgetCpde: 1234

路徑里包含子路徑也是可以提取出來的:

更多Jerry的原創文章,盡在:“汪子熙”:

總結

以上是生活随笔為你收集整理的如何对SAP Spartacus支持路由的Component进行单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。