如何在jasmine中的observable中为http post编写单元测试?
我有web.service.ts,我想为此编写一个单元测试。但我有一种方法如何在jasmine中的observable中为http post编写单元测试?,jasmine,observable,angular7,karma-runner,Jasmine,Observable,Angular7,Karma Runner,我有web.service.ts,我想为此编写一个单元测试。但我有一种方法 updateClientConfiguration(id, data){ var res = this.http.post<any> (`${this.configUrl}/${this.helper.getNodeIP()}/clients/${id}`,data); //1 res.subscribe(result => {
updateClientConfiguration(id, data){
var res = this.http.post<any>
(`${this.configUrl}/${this.helper.getNodeIP()}/clients/${id}`,data); //1
res.subscribe(result => { //2
if(data.type == "aws")
{
console.log("Updating AWS UI");
}
else if(data.type == "azure")
{
console.log("Updating AZURE UI");
data[data.type].sasToken = tmpData;
}
else if(data.type == "local_lake")
{
console.log("Updating LOCAL_LAKE UI");
}
});
return res;
}
updateClientConfiguration(id、数据){
var res=this.http.post
(`${this.configUrl}/${this.helper.getNodeIP()}/clients/${id}`,数据);//1
res.subscribe(结果=>{//2
如果(data.type==“aws”)
{
console.log(“更新AWS UI”);
}
else if(data.type==“azure”)
{
日志(“更新AZURE UI”);
数据[data.type].sasToken=tmpData;
}
else if(data.type==“本地_湖”)
{
console.log(“更新本地_湖用户界面”);
}
});
返回res;
}
我想通过检查每个“if”块中的控制台消息来获得此代码的100%覆盖率。如何在res.subscribe{}块内进行测试?只需模拟http.post并调用函数即可
let httpClient : HttpClient;
httpClient = TestBed.get(HttpClient);
spyOn(httpClient,"post").and.returnValue(of(new HttpResponse()));
service.updateClientConfiguration(data.clientId,data).subscribe(response => {
expect(consoleSpy.calledWith(consoleMsg)).toBe(true);
});