我正在学习angular和单元测试,似乎无法解决这个问题。也许我做的一切都错了,但我想测试一下是否进行了http调用。
app.service.ts:
transformData(data):Promise<any>{
return new Promise((resolve, reject) => {
this.http.post<any>('https://www.example.com',data).subscribe(
resp => {
resolve(resp);
},
err => {
reject(err);
}
);
});
}
我现在的测试是:
fit("should submit data for processing", fakeAsync(() => {
const service = TestBed.get(AppService);
let response = {
processedData: 100
};
service
.transformData({'data':'data'})
.then(result => {
expect(result).toEqual(response);
});
// Expect a call to this URL
const req = httpTestingController.expectOne(
"https://www.example.com/"
);
expect(req.request.method).toEqual("POST");
req.flush(response);
tick();
}));
上面写着:
Error: Expected one matching request for criteria "Match URL: https://www.example.com/", found none.
发布于 2020-02-29 01:17:02
我会这样测试它:
fit("should submit data for processing", async(done) => {
const service = TestBed.get(AppService);
let response = {
processedData: 100
};
service
.transformData({'data':'data'})
.then(result => {
expect(result).toEqual(response);
// call done() to tell the test we are done with our assertions
done();
});
// Expect a call to this URL
const req = httpTestingController.expectOne("https://www.example.com");
expect(req.request.method).toEqual("POST");
req.flush(response);
});
https://stackoverflow.com/questions/60456324
复制相似问题