[英]How to mock router.navigateByUrl(…).then in Angular with Karma and Jasmine
My problem is pretty simple, I use navigateByUrl() then then() in this way:我的问题很简单,我以这种方式使用 navigateByUrl() 然后 then() :
this.router
.navigateByUrl(PAGE1, {
skipLocationChange: true
})
.then(() => {
this.router.navigate([PAGE2]);
});
and I mock navigate, navigateByUrl and then this way:我模拟了导航,navigateByUrl 然后这样:
const mockRouter = {
navigate: () => {},
navigateByUrl(url: string) { return url; },
then: () => {}
};
It works well for navigate and navigateByUrl but it doesn't work for then and I don't know how to mock this function.它适用于导航和导航ByUrl,但不适用于那时,我不知道如何模拟这个 function。 How can I proceed?
我该如何进行?
Thanks to jonrsharpe advise i found the solution.感谢 jonrsharpe 的建议,我找到了解决方案。 The solution was to mock navigateByUrl this way:
解决方案是以这种方式模拟 navigateByUrl:
navigateByUrl: () => {
return {
then: () => {}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.