0

问题

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

服务:

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

我的单元测试:

带模拟课

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

...

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

我试过差异。方法来自:

Angular2 RC5 模拟激活路由参数

Angular2如何模拟订阅的激活路由参数

单元测试角度正确激活路线

Angular 4 - 失败:无法解析 ActivatedRoute 的所有参数:(?, ?, ?, ?, ?, ?, ?, ?)

但我没有得到任何地方。

呃:

route.params.pipe is not a function
4

1 回答 1

2

你试过这样吗?

class ActivatedRouteMock {
    params = of( { modelId: 'test1' } )
}
于 2020-03-03T10:47:53.713 回答