how can i create MockActivatedRoute for the parent routes of ActivatedRoute in angular 4 Unit testing

我的未来我决定 提交于 2019-12-11 07:19:39

问题


Lets say I have a component that access parent route like this:

ngOnInit() {    
this.sub = this.route.parent.parent.parent.params.subscribe(params => {
    this.siteId = params["siteId"];
    this.loadContents();
    this.loadTypes(this.siteId);
});}

I m providing fake value for parent route like this:

 providers: [
        SiteService,
        UserService,
        Modal,
        Overlay,
        {
            provide: ActivatedRoute,
            useValue: { parent: { parent: { parent: { 'params': Observable.from([{ 'siteId': '156' }]) } } } },
        },
        OverlayRenderer,
        AuthHttp,
        MessageService,
    ],

it is giving me following error:

Failed: Cannot read property 'undefined' of undefined

AT line:

useValue: { parent: { parent: { parent: { 'params': Observable.from([{ 'siteId': '156' }]) } } } },

Please tell me what it is that i am doing in correct. i have referenced following link:

How to mock an activatedRoute parent Route, Testing Angular 2 parent route

来源:https://stackoverflow.com/questions/45789131/how-can-i-create-mockactivatedroute-for-the-parent-routes-of-activatedroute-in-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!