簡體   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