我正在尝试使用 sinon 测试我的节点 js api。我有一个 返回 appDetails的函数appRequest() ;但我无法得到结果。我正在使用 proxyquire 来获得结果,但它不起作用。
appService.js
function appRequest(apprequestData) {
return new Promise(function (resolve, reject) {
console.log(apprequestData);
axios.get(url + apprequestData.dataId).then(appResult => {
console.log('appResult'+appResult);
if ('' == appResult.data.result || appResult.data.result[0].dataId!= apprequestData.dataId) {
reject({
status: 404,
message: "APP not Found"
});
return;
}
resolve({
clientId: appResult.data.result[0].dataId
});
}).catch(error => {
console.log(error);
if (null == error.response || error.response.status == 404) {
reject({ status: 404, message: "APP service down", referenceNumber: apprequestData.referenceNumber });
return;
}
reject({ status: 404, message: "APP service down", referenceNumber: apprequestData.referenceNumber });
return;
});
});
}
app.test.js
describe("应用请求", function() {
describe('get app details', () =>{
var appDetails, teststub;
let req = {
"dataId": "1234"
}
let response={ "dd":23}
// teststub = sinon.stub(axios,'get').resolves(Promise.resolve(response));
teststub = sinon.stub();
it('should return app ', function() {
appDetails = proxyquire('../app/service/appService', { 'axios': teststub.returns(response)})
console.log(appDetails);
var result=appDetails.appRequest(req);
console.log("***"+JSON.stringify(result)); // this is coming as empty
expect(result.statusCode).equal(200);
});
});
});