我有一个服务方法,它有一个服务调用(HTTP 调用),它立即订阅,并根据响应代码执行其余的操作命令。
示例:服务方法
processData(id): void {
const url = `http://localhost:5000/data/${id}`;
this.http.head(url).subscribe(() => {
console.log('Success');
// Rest of the code - TODO
}, (error) => {
console.log('Failed');
// Rest of the code - TODO
});
}
我尝试了以下示例(测试用例)
fdescribe('ReportedFileService', () => {
let service: DataService;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports:[HttpClientModule, HttpClientTestingModule],
providers:[DataService]
});
service = TestBed.get(DataService);
httpMock = TestBed.get(HttpTestingController);
});
afterEach(() => {
httpMock.verify();
});
fit('should be valid', () => {
const id = 1;
const filePath = `http://localhost:5000/data/${id}`;
const req = httpMock.expectOne(filePath);
expect(req.request.method).toEqual('Head');
const response = service.processData(id);
})
}
请帮助我如何处理这种情况。