Angular 如果我在服务中添加subscribe和该服务的单元测试,我';我得到了财产';订阅';不存在于类型';订阅';?
这是我的服务 我的服务.service.tsAngular 如果我在服务中添加subscribe和该服务的单元测试,我';我得到了财产';订阅';不存在于类型';订阅';?,angular,unit-testing,subscription,subscribe,Angular,Unit Testing,Subscription,Subscribe,这是我的服务 我的服务.service.ts export class MyService { constructor(public http: HttpClient) {} createChatRecord (data, id) { let logData = {"hist" : data , "uid": id }; let headers = new HttpHeaders(); headers = headers.set(
export class MyService {
constructor(public http: HttpClient) {}
createChatRecord (data, id) {
let logData = {"hist" : data , "uid": id };
let headers = new HttpHeaders();
headers = headers.set("Content-Type", "application/json");
let options = { headers: headers };
return this.http.post(MY_URL, logData, options)
.subscribe(res => res);
}
}
it("It should post", () => {
myservice.createChatRecord(mockData, mockId)
.subscribe((data: any) => {
expect(data).toBe(null);
});
const mockReq = httpMock.expectOne(MY_URL);
expect(mockReq.request.method).toBe("POST");
mockReq.flush(null);
httpMock.verify();
});
createChatRecord (data, id) {
let logData = {"hist" : data , "uid": id };
let headers = new HttpHeaders();
headers = headers.set("Content-Type", "application/json");
let options = { headers: headers };
return this.http.post(MY_URL, logData, options);
}
这是我的上述服务的规范文件
我的服务规范ts
export class MyService {
constructor(public http: HttpClient) {}
createChatRecord (data, id) {
let logData = {"hist" : data , "uid": id };
let headers = new HttpHeaders();
headers = headers.set("Content-Type", "application/json");
let options = { headers: headers };
return this.http.post(MY_URL, logData, options)
.subscribe(res => res);
}
}
it("It should post", () => {
myservice.createChatRecord(mockData, mockId)
.subscribe((data: any) => {
expect(data).toBe(null);
});
const mockReq = httpMock.expectOne(MY_URL);
expect(mockReq.request.method).toBe("POST");
mockReq.flush(null);
httpMock.verify();
});
createChatRecord (data, id) {
let logData = {"hist" : data , "uid": id };
let headers = new HttpHeaders();
headers = headers.set("Content-Type", "application/json");
let options = { headers: headers };
return this.http.post(MY_URL, logData, options);
}
如果我在spec文件中添加subscribe,就会出现此错误。
类型“订阅”上不存在属性“订阅”。但如果我删除服务中的订阅,它不会显示该错误
我需要在我的服务中使用subscribe。如何在没有此错误的情况下测试此post调用
任何帮助都将不胜感激 您的服务类中的
subscribe()
用法不正确。如果您删除该用法,您将消除该错误
createChatRecord(数据,id){
让logData={“hist”:data,“uid”:id};
let headers=新的HttpHeaders();
headers=headers.set(“内容类型”、“应用程序/json”);
let options={headers:headers};
返回this.http.post(我的URL、日志数据、选项);
}
正如Daniel所建议的,我从服务中删除了subscribe,并在调用createChatRecord的方法中添加了服务
调用createChatRecord的My函数
myfunction() { // To make call record in service now
this.servicenow.createChatRecord(this.conversationData, this.userId)
.subscribe();
}
myservice.service.ts
export class MyService {
constructor(public http: HttpClient) {}
createChatRecord (data, id) {
let logData = {"hist" : data , "uid": id };
let headers = new HttpHeaders();
headers = headers.set("Content-Type", "application/json");
let options = { headers: headers };
return this.http.post(MY_URL, logData, options)
.subscribe(res => res);
}
}
it("It should post", () => {
myservice.createChatRecord(mockData, mockId)
.subscribe((data: any) => {
expect(data).toBe(null);
});
const mockReq = httpMock.expectOne(MY_URL);
expect(mockReq.request.method).toBe("POST");
mockReq.flush(null);
httpMock.verify();
});
createChatRecord (data, id) {
let logData = {"hist" : data , "uid": id };
let headers = new HttpHeaders();
headers = headers.set("Content-Type", "application/json");
let options = { headers: headers };
return this.http.post(MY_URL, logData, options);
}
因此,我了解到,如果我订阅该服务,它将返回一个订阅且不可见,这就是我出现此错误的原因。但如果我删除该订阅,它将不会进行网络呼叫。在服务中添加subscribe是错误的吗?是的,这样做是错误的。调用
subscribe()
方法时将发出网络请求。你需要在你的单元测试中订阅来测试它,在你调用createChatRecord(…)
的代码中你也需要订阅。非常感谢。你能不能用一个示例片段或其他东西更新你的答案,让我更清楚?你有没有阅读过Angular教程来了解如何做到这一点?他们很好地展示了如何设置和使用您正在创建的服务,并向您展示了如何做到这一点。以下是本教程中详细介绍如何执行此操作的具体步骤: