繁体   English   中英

如何使用 Karma 和 Jasmine 在 Angular 中模拟 router.navigateByUrl(...).then

[英]How to mock router.navigateByUrl(…).then in Angular with Karma and Jasmine

我的问题很简单,我以这种方式使用 navigateByUrl() 然后 then() :

this.router
.navigateByUrl(PAGE1, {
  skipLocationChange: true
})
.then(() => {
  this.router.navigate([PAGE2]);
});

我模拟了导航,navigateByUrl 然后这样:

const mockRouter = {
  navigate: () => {},
  navigateByUrl(url: string) { return url; },
  then: () => {}
};

它适用于导航和导航ByUrl,但不适用于那时,我不知道如何模拟这个 function。 我该如何进行?

感谢 jonrsharpe 的建议,我找到了解决方案。 解决方案是以这种方式模拟 navigateByUrl:

navigateByUrl: () => {
    return {
      then: () => {}
    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM