gpt4 book ai didi

javascript - 测试异步调用会在 Node JS 中抛出未处理的 promise 拒绝

转载 作者:太空宇宙 更新时间:2023-11-04 02:04:20 24 4
gpt4 key购买 nike

我正在对这样的 Express 函数进行单元测试:

function(request,response,someDependency){
...
someDependency.doStuff(arg1,arg2)
.then(function(someResponse){
response.status(200).send({body: someResponse});
})
.catch(function(error){
response.status(500).send({someResponse: 'error');
});
}

在我的单元测试(使用 Mocha)中,我决定使用其中的断言来模拟我的响应,并模拟我的依赖项:

我的mockedResponse 的抽象:

...
exports.expectedStatus = function(status){
this.expectedCode = status;
}
exports.status = function(realStatus){
this.expectdCode = realStatus;
return this;
}
exports.send = function(body){
assert.strictEqual(this.realStatus,this.expectedCode);
}

我的依赖关系的抽象是:

exports.setPromiseError(error){
this.error = error;
};
exports.doStuff(arg1,arg2){
return new Promise(function (resolve, reject){
if (!error)
resolve({this: 'is', a: 'response'});
else
reject(error);
}});

在我的单元测试中,我有一个这样的测试:

mockedResponse = require('./fake/assertiveResponse.js');
mockedDependency = require('./fake/dependency.js');

it('should throw error',function(){
mockedDependency.setPromiseError(new Error('sorry, not today');
myLibUnderTest.myFunctionUnderTest(someFakeRequest,
mockedResponse.get()
.expectedStatus(200), <--I´m forcing a failing test here!
mockedDependency);
});

但是...我的断言似乎是 swalled按照 promise ,我通过了测试,控制台输出如下:

(node:1512) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 2): AssertionError: 500 === 200

现在我什至不确定我是否以正确的方式使用 Promise...任何帮助都会受到赞赏。

最佳答案

您应该将 Promise 视为表示您尚未拥有的数据的对象。

就我个人而言,我对 Promise 的大部分经验都来自于使用 jQuery 的 $.ajax 方法,该方法实际上返回自己的“promise”或“deferred”对象:

$.ajax({
url: "api.yourdomain.com/yourRoute",
success: function(data, textStatus, jqXHR){
//this is the success callback

},
error: function(jqXHR, textStatus, errorThrown){
//this is the error callback
}
});

现在,假设您需要调用电话来获取一些客户数据,并将该 promise 传递给不同的方法。同样,使用 jQuery,您可以执行类似的操作

function getCustomerDataPromise(customerID){
return $.ajax({
url: "customerapi.yourdomain.com/customer/" + customerID,
success: function(data, textStatus, jqXHR){
//this is the success callback

},
error: function(jqXHR, textStatus, errorThrown){
//this is the error callback
}
});
}


function parseCustomerData(customerID){
var customerDataPromise = getCustomerDataPromise(customerID);

$.when(customerDataPromise).done(
function(customerDataResponse, responseText, responseObject){
//this is the success callback of the promise

}
);

$.when 最酷的一点是,您实际上可以在其中传递多个 Promise,并在所有 Promise 完成时触发回调。

但是为了回答你原来的问题,你可以模拟一个 promise ,也使用 jQuery,如下所示:

function makeMockedPromise(){
var dummyPromise = $.Deferred();
dummyPromise.resolve(
{
mockedKey: "Is this mocked data?",
mockedKeyTwo: "You bet it is! A mocked response"
}
);
return dummyPromise;
}

您可以在 jQuery 文档中阅读有关此内容的更多信息:http://api.jquery.com/

关于javascript - 测试异步调用会在 Node JS 中抛出未处理的 promise 拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44877038/

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