"TypeError: Cannot read property 'pipe' of undefined" - Do I miss additional setup in my unit tests ( Angular / Unit testing / ActivatedRoute )

"TypeError: Cannot read property 'pipe' of undefined" - Do I miss additional setup in my unit tests ( Angular / Unit testing / ActivatedRoute )

我目前面临一个问题,我有一个基于 activatedRoute 的 paramMap 的 Observable

this.current$ = this.route.paramMap.pipe(map(paramMap => paramMap.get('title')));

它在我的前端运行良好,但我最近开始使用 angular 单元测试,并且我有一个非常基本的测试可以开始。目前仅持有:


  beforeEach(() => {
    fixture = TestBed.createComponent(BannerComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeDefined();
  });

  it('should create', () => {
    expect(component).toBeDefined();
  });

出于某种原因,当运行我的测试。以下是错误的。

TypeError: Cannot read property 'pipe' of undefined

我还有其他一些简单的测试设置,但它们都因上述消息而失败。我在这里错过了什么吗?

提前致谢。

您可以尝试像这样模拟路线:

 `` 
    {
      provide: ActivatedRoute,
      useValue: {
        snapshot: {
          paramMap: {
            get: (key: string) => {
              switch (key) {
                case 'title':
                  return 'my title';
                case 'subtitle':
                  return 'my subtitle'
              }
            }
          },
        },
      },
    }``