gpt4 book ai didi

Angular 8 HttpTestingController 错误 - 'Cannot flush a cancelled request.'

转载 作者:行者123 更新时间:2023-12-05 07:13:40 56 4
gpt4 key购买 nike

我的测试遇到了一个真正的问题,而我已经成功运行并模拟并返回的 HTTP 调用现在没有从测试中刷新并导致测试失败。我得到的错误是

Cannot flush a cancelled request.

实际测试本身是在默认测试框架内使用 Jest/Cucumber,这一切都正常工作,所以问题不在于此。

我测试的“然后”部分如下:

then(
/^the payment authority should be sent as '(.*?)'$/,
fakeAsync(consent => {
tick();
flushBrandPropertyCall(httpTestingController);
flushQuoteStatusCall(httpTestingController);
flushReferenceDataCall(httpTestingController);
httpTestingController.verify(); //Similar to an assert just make sure page is stable before continuing

const submitBtn = fixture.debugElement.query(By.css('#continue-button')).nativeElement;
submitBtn.click();

const timeRequest = httpTestingController.expectOne(HttpUrl.Time);
console.log('Cancelled?', timeRequest.cancelled);
if (!timeRequest.cancelled) timeRequest.flush({ now: Date.now() });
flushBrandPropertyCall(httpTestingController);
tick(1500); // Not sure exact source - maybe modal messages?
const payRequest = httpTestingController.expectOne(HttpUrl.SinglePayment);
expect(payRequest.request.body.paymentAuthority).toEqual(consent);
payRequest.flush({ paymentStatus: 'CARD_PAYMENT_SUCCESS', authCode: '', policyNumber: 5555, quoteVersion: 20 });
httpTestingController.verify();
expect(true).toEqual(true);
})
);
});

正在取消失败的 HTTP 调用是 flushBrandPropertyCall),如下所示:

export const flushBrandPropertyCall = (controller: HttpTestingController) => {
const request = controller.expectOne('/web-services/rest/properties/removedForSecurity');
console.log(request)

request.flush([
{
id: 8,
brand: 'removedForSecurity',
key: 'openingTimes',
value:
'{"motor":{"SAT":{"from":"09:00","to":"18:00"},"MON":{"from":"08:00","to":"22:00"},"TUE":{"from":"08:00","to":"22:00"},"WED":{"from":"08:00","to":"22:00"},"THU":{"from":"08:00","to":"22:00"},"FRI":{"from":"08:00","to":"22:00"},"SUN":{"from":"10:00","to":"18:00"},"YYYY-12-25":"holiday","YYYY-12-26":"holiday","2017-01-02":{"from":"10:00","to":"18:00"}},"home":{"SAT":{"from":"09:00","to":"17:00"},"SUN":{"from":"10:00","to":"16:00"},"WEEKDAY":{"from":"08:00","to":"22:00"},"YYYY-12-25":"holiday","YYYY-12-26":"holiday","2017-01-02":{"from":"10:00","to":"18:00"}},"multicover":{"SAT":{"from":"09:00","to":"17:00"},"SUN":{"from":"10:00","to":"16:00"},"WEEKDAY":{"from":"08:00","to":"22:00"},"YYYY-12-25":"holiday","YYYY-12-26":"holiday","2017-01-02":{"from":"10:00","to":"18:00"}},"van":{"SAT":{"from":"09:00","to":"17:00"},"MON":{"from":"08:00","to":"21:00"},"TUE":{"from":"08:00","to":"21:00"},"WED":{"from":"08:00","to":"21:00"},"THU":{"from":"08:00","to":"21:00"},"FRI":{"from":"08:00","to":"21:00"},"SUN":{"from":"10:00","to":"16:00"},"YYYY-12-25":"holiday","YYYY-12-26":"holiday","2017-01-02":{"from":"10:00","to":"18:00"}}}'
},
{
id: 364,
brand: 'aaaaa',
key: 'csOpeningTimes',
value:
'{"motor":{"SAT":{"from":"09:00","to":"17:00"}, "SUN":{"from":"10:00","to":"16:00"},"MON":{"from":"08:00","to":"20:00"},"TUE":{"from":"08:00","to":"20:00"},"WED":{"from":"08:00","to":"20:00"},"THU":{"from":"08:00","to":"20:00"},"FRI":{"from":"08:00","to":"20:00"}},"home":{"SAT":{"from":"09:00","to":"17:00"},"SUN":{"from":"10:00","to":"16:00"},"MON":{"from":"08:00","to":"20:00"},"TUE":{"from":"08:00","to":"20:00"},"WED":{"from":"08:00","to":"20:00"},"THU":{"from":"08:00","to":"20:00"},"FRI":{"from":"08:00","to":"20:00"}},"van":{"SAT":{"from":"09:00","to":"17:00"},"SUN":{"from":"10:00","to":"16:00"},"MON":{"from":"08:00","to":"21:00"},"TUE":{"from":"08:00","to":"21:00"},"WED":{"from":"08:00","to":"21:00"},"THU":{"from":"08:00","to":"21:00"},"FRI":{"from":"08:00","to":"21:00"}}}'
},
{
id: 91,
brand: 'aaaaaa',
key: 'publicKey',
value:
'10001|AD0FA4B69A67FA53084A6208E9E248D33EC1251C1B091893CA7855672755352A44C372F6999CEA01BD9B34F4A08E64A1A8009E1596E4B992FCCB6888E721E6CF1DA3F5918DCDB7F9B461823CCACF867671B32CE487236D24BEB93CE448E9541537CC18668B0CCE60E9566D5E00F62C1636606ABCBA38DADFF9A898D37388852D698A27388849A4C5A9394233E55B14541A811C228888239A7B55C9F782F0C0EE56D283AA74F11025AF0E3A08C8CA545505853F0060D61F003DAD78B57EE53C86395EB6BF013CA2C85FD9B4E21C062770BFDC02EA3315CD60FC4ED8F83E4108B8479BB0DA2ADC6E84029026FBB4ACEB123220C174DE7821A01EAA2E98D3342E03'
}
]);
tick();
};

我试图在网上四处看看,但我终其一生都找不到关于该错误的任何信息。有谁知道为什么使用 HttpTestingController 从测试规范中取消请求?

我还控制台记录了 requestOne,这是请求对象

 TestRequest {
request: HttpRequest {
url: '/web-services/rest/properties/removedForSecurity',
body: null,
reportProgress: false,
withCredentials: false,
responseType: 'json',
method: 'GET',
headers: HttpHeaders {
normalizedNames: Map {},
lazyUpdate: null,
headers: Map {}
},
params: HttpParams {
updates: null,
cloneFrom: null,
encoder: HttpUrlEncodingCodec {},
map: null
},
urlWithParams: '/web-services/rest/properties/removedForSecurity'
},
observer: InnerSubscriber {
closed: true,
_parentOrParents: null,
_subscriptions: null,
syncErrorValue: null,
syncErrorThrown: false,
syncErrorThrowable: false,
isStopped: true,
destination: {
closed: true,
next: [Function: next],
error: [Function: error],
complete: [Function: complete]
},
parent: MergeMapSubscriber {
closed: true,
_parentOrParents: null,
_subscriptions: null,
syncErrorValue: null,
syncErrorThrown: false,
syncErrorThrowable: true,
isStopped: true,
destination: [FilterSubscriber],
project: [Function],
concurrent: 1,
hasCompleted: true,
buffer: [],
active: 1,
index: 1
},
outerValue: undefined,
outerIndex: undefined,
index: 1
},
_cancelled: true
}

最佳答案

刷新请求尝试在正文之后添加:

request.flush([...], {headers: {'Content-Type': 'application/json'}})

关于Angular 8 HttpTestingController 错误 - 'Cannot flush a cancelled request.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60094785/

56 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com