我检查了很多文章和答案,但似乎没有找到正确的方法来模拟HTTP Requests
对于我的方法。我想测试我的frontend
应用程序独立于backend
。这是我拥有的方法类型:
private getProfile() {
this.http
.get('go/profile/get', {withCredentials: true})
.subscribe((profile: Profile) => {
this.user.profile = profile;
this.updateLineMsgs();
});
}
有什么建议 ?
您始终可以自己创建一个模拟方法并模拟您期望从后端获得的响应。在你的例子中它可能是
public static mockGetProfile(){
const response = JSON.parse(`
"name": "abc",
"active": true,
...all other json fields that you want
`);
let obs = new Observable((subscriber) => {
setTimeout(()=>{
subscriber.next(response);
subscriber.complete();
}, 3000);
});
return obs;
}
上面的可观察结果将在 3 秒或您定义的任何时间段后完成,以某种方式模拟来自后端服务器的响应,这需要一些时间才能可用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)