How to unit test a component that depends on parameters from ActivatedRoute?

前端 未结 8 1116
离开以前
离开以前 2020-12-04 09:37

I am unit testing a component that is used to edit an object. The object has an unique id that is used in order to grab the specific object from an array of obj

8条回答
  •  生来不讨喜
    2020-12-04 09:54

    Just add a mock of the ActivatedRoute:

    providers: [
      { provide: ActivatedRoute, useClass: MockActivatedRoute }
    ]
    

    ...

    class MockActivatedRoute {
      // here you can add your mock objects, like snapshot or parent or whatever
      // example:
      parent = {
        snapshot: {data: {title: 'myTitle ' } },
        routeConfig: { children: { filter: () => {} } }
      };
    }
    

提交回复
热议问题