刷新一系列 Angular HTTP TestRequest
Flushing a sequence of Angular HTTP TestRequest
我有一个 async
函数需要发出 POST
请求,但有些数据必须先从服务器获取:
async createObject() {
// HTTP request #1: dep1 is needed for the second request
const dep1 = await this.http.get("/urlA")
.pipe(first())
.toPromise();
// Create the request object out of dep1 and some additional values
const req = mkCreateParams(this.name, dep1);
// HTTP request #2: Build something
const res = await this.http.post("/urlB")
.pipe(first())
.toPromise();
// When everything is done: Navigate away
this.router.navigateByUrl(`/urlC/${res.id}`);
return res;
}
我使用以下代码对此进行测试:
const httpTesting = TestBed.inject(HttpTestingController);
const req = testInstance.createObject();
httpTesting
.expectOne("/urlA")
.flush({ /* ... SNIP ... */);
const generatedId = "f9f64792-0ceb-4e3c-ae7b-4c7a8af6a552";
httpTesting
.expectOne({ method: "POST", url: "/urlB" })
.flush({ id: generatedId });
const res = await req;
expect(res.id).toEqual(generatedId);
这在期望 /urlB
时立即失败,甚至没有到达解析 res
的行。报错信息如下:
Expected one matching request for criteria "Match method: POST, URL: /urlB", found none.
这似乎是因为在调用 HttpTestingController.expectOne()
时必须已经发出请求。由于在 JavaScript 中急切地解决了 promises,所以第一个请求已经发出,但第二个请求还没有。
有没有办法告诉 Angular HttpTestingController
稍微放松一下,等待请求稍后到来? HttpTestingController.verify
的存在暗示了这一点,但我不知道如何进入有用的状态。
我觉得等待承诺对你有帮助,试试fixture.whenStable()
。
it('your title here', async done => {
const httpTesting = TestBed.inject(HttpTestingController);
const req = testInstance.createObject();
httpTesting
.expectOne("/urlA")
.flush({ /* ... SNIP ... */);
await fixture.whenStable(); // wait for the pending promises to resolve before proceeding
const generatedId = "f9f64792-0ceb-4e3c-ae7b-4c7a8af6a552";
httpTesting
.expectOne({ method: "POST", url: "/urlB" })
.flush({ id: generatedId });
await fixture.whenStable(); // wait for the pending promises to resolve before proceeding
const res = await req;
expect(res.id).toEqual(generatedId);
// call done to let Jasmine know you're done with this test
done();
});
我有一个 async
函数需要发出 POST
请求,但有些数据必须先从服务器获取:
async createObject() {
// HTTP request #1: dep1 is needed for the second request
const dep1 = await this.http.get("/urlA")
.pipe(first())
.toPromise();
// Create the request object out of dep1 and some additional values
const req = mkCreateParams(this.name, dep1);
// HTTP request #2: Build something
const res = await this.http.post("/urlB")
.pipe(first())
.toPromise();
// When everything is done: Navigate away
this.router.navigateByUrl(`/urlC/${res.id}`);
return res;
}
我使用以下代码对此进行测试:
const httpTesting = TestBed.inject(HttpTestingController);
const req = testInstance.createObject();
httpTesting
.expectOne("/urlA")
.flush({ /* ... SNIP ... */);
const generatedId = "f9f64792-0ceb-4e3c-ae7b-4c7a8af6a552";
httpTesting
.expectOne({ method: "POST", url: "/urlB" })
.flush({ id: generatedId });
const res = await req;
expect(res.id).toEqual(generatedId);
这在期望 /urlB
时立即失败,甚至没有到达解析 res
的行。报错信息如下:
Expected one matching request for criteria "Match method: POST, URL: /urlB", found none.
这似乎是因为在调用 HttpTestingController.expectOne()
时必须已经发出请求。由于在 JavaScript 中急切地解决了 promises,所以第一个请求已经发出,但第二个请求还没有。
有没有办法告诉 Angular HttpTestingController
稍微放松一下,等待请求稍后到来? HttpTestingController.verify
的存在暗示了这一点,但我不知道如何进入有用的状态。
我觉得等待承诺对你有帮助,试试fixture.whenStable()
。
it('your title here', async done => {
const httpTesting = TestBed.inject(HttpTestingController);
const req = testInstance.createObject();
httpTesting
.expectOne("/urlA")
.flush({ /* ... SNIP ... */);
await fixture.whenStable(); // wait for the pending promises to resolve before proceeding
const generatedId = "f9f64792-0ceb-4e3c-ae7b-4c7a8af6a552";
httpTesting
.expectOne({ method: "POST", url: "/urlB" })
.flush({ id: generatedId });
await fixture.whenStable(); // wait for the pending promises to resolve before proceeding
const res = await req;
expect(res.id).toEqual(generatedId);
// call done to let Jasmine know you're done with this test
done();
});