在 Angular 中使用参数模拟激活的路由

Mock Activated Route With Params in Angular

问题

如何在我的单元测试中模拟 ActivatedRoutes

服务:

  constructor(
    private route: ActivatedRoute,
    private knowledgeModelService: KnowledgeModelService
  ) {
    route.params.pipe(map(p => p.modelId)).subscribe((modelId) => {
      this.modelId = modelId;
    });
  }

我的单元测试:

模拟class

  class ActivatedRouteMock {
    // params = of([{modelId: 'test1'}]);
    params = { paramMap: of( convertToParamMap( { modelId: 'test1' } ) ) }
  }

...

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ StatsComponent ],
      providers: [
        {
          provide: ActivatedRoute, useClass: ActivatedRouteMock
        },

我试过diff。方法来自:

Unit test angular activate route correctly

但我什么也做不了。

错误:

route.params.pipe is not a function

你试过吗?

class ActivatedRouteMock {
    params = of( { modelId: 'test1' } )
}