gpt4 book ai didi

javascript - Mocha 测试 Http Request Promise 是否抛出 : Unhandled promise rejection

转载 作者:行者123 更新时间:2023-11-28 04:07:18 26 4
gpt4 key购买 nike

我正在尝试测试我的 Http Request 方法是否抛出错误,但我总是得到:未处理的 promise 拒绝

这是我的测试:

    it('it should get the first page of all offers with limit 20 without a cache system', (done) => {
const httpRequestConnector = new HttpRequestConnector(apiConfigs);
const queryArgs: any = {
page: 1,
limit: 20,
};

const functionThatThrows = () => {
httpRequestConnector.offersFindAll(queryArgs, 'admin').then((res) => {
res.should.have.property('status');
res.status.should.equal(200);
res.should.have.property('body');

res.body.should.have.property('request');
res.body.request.should.have.property('page');
res.body.request.page.should.equal('1');
res.body.request.should.have.property('limit');
res.body.request.limit.should.equal('20');

res.body.should.have.property('response');
res.body.response.should.have.property('data');
res.body.response.data.should.have.property('data');

Object.keys(res.body.response.data.data).length.should.equal(20);
}).catch((err) => {
throw err;
});
};

expect(functionThatThrows).to.throw();
done();
});

这是我的方法(其中“受约人”会导致错误):

...
private query(url: string, send: any): Promise<any> {
const httpRequestService: HttpRequestInterface = new HttpRequestService();

return httpRequestService.post({
url,
send,
});
}

offersFindAll(args: any, apiDefinition: string): Promise<any> {
(new GuardAgainstNullValues())
.guard(apiDefinition);

const queryArgs = args || {};
const target: string = (apiDefinition === 'admin') ? 'Offeree' : 'Affiliate_Offer';
const method: string = 'findAll';
const apiInfos: any = this.getApiInfos(apiDefinition);
const url: string = apiInfos.api + '?api_key=' + apiInfos.api_key + '&Target=' + target + '&Method=' + method;

if (queryArgs && 'limit' in queryArgs) {
queryArgs.limit = args.limit;
} else {
queryArgs.limit = 1000;
}

return new Promise((fulfill, reject) => {
return this.query(url, queryArgs)
.then((res) => {
if (res.body.response.status === -1) {
throw new RequestStatusException('Cannot get offers');
}

fulfill(res);
})
.catch((err) => {
reject(err);
});
});
}
...

这是我的 HttpRequestService 的 POST 方法:

class HttpRequestService implements HttpRequestInterface{
private engine: SuperAgentStatic;

constructor() {
this.engine = request; // Superagent
}

get({ url, query }: { url: string, query?: any }): Promise<any>{
return new Promise((fulfill: any, reject: any) => {
this.engine
.get(url)
.query(query)
.end((err: ResponseError, res: Response) => {
if (err) {
reject(err);
}

fulfill(res);
});
});
}

post({ url, send }: { url: string, send?: any }): Promise<any>{
return new Promise((fulfill: any, reject: any) => {
this.engine
.post(url)
.send(qs.stringify(send))
.end((err: ResponseError, res: Response) => {
if (err) {
reject(err);
}

fulfill(res);
});
});
}
};

我包装了 OfferFindAll 请求,并 promise 处理 if (res.body.response.status === -1) { 来抛出错误,而不是将其抛出到我使用此方法的任何地方。

我试图期望该函数在我的 Mocha 测试中抛出:expect(functionThatThrows).to.throw();

但我总是得到一个(node:7485) UnhandledPromiseRejectionWarning: Unhandled Promise Rejection (rejection id: 1): RequestStatusException: Cannot get Offers 这是一个很好的异常(exception),但我无法处理正确地 promise 。

我尝试只返回 this.query 而不将其包装在 promise 中,但做了同样的事情。

如何正确构建它来测试它是否抛出?

非常感谢您的帮助

最佳答案

在你的方法中offesFindAll():

    return new Promise((fulfill, reject) => {
return this.query(url, queryArgs)
.then((res) => {
if (res.body.response.status === -1) {
throw new RequestStatusException('Cannot get offers');
}

fulfill(res);
})
.catch((err) => {
reject(err);
});
});

catch 子句无法捕获该异常。最好这样写:

    return new Promise((fulfill, reject) => {
return this.query(url, queryArgs)
.then((res) => {
if (res.body.response.status === -1) {
reject('Cannot get offers');
}

fulfill(res);
})
.catch((err) => {
reject(err);
});
});

同时,可以使用以下方式找到错误的堆栈跟踪:

process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at:', p, 'reason:', reason);
// application specific logging, throwing an error, or other logic here
});

关于javascript - Mocha 测试 Http Request Promise 是否抛出 : Unhandled promise rejection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46586565/

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