【发布时间】:2019-06-01 09:36:57
【问题描述】:
我想在节点模块中模拟一个函数的结果,以便我可以运行断言。 考虑以下节点模块:
const doPostRequest = require('./doPostRequest.js').doPostRequest;
const normalizeSucessResult = require('./normalizer.js').normalizeSucessResult;
const normalizeErrorResult = require('./normalizer.js').normalizeErrorResult;
exports.doPost = (params, postData) => {
return doPostRequest(params, postData).then((res) => {
const normalizedSuccessResult = normalizeSucessResult(res);
return normalizedSuccessResult;
}).catch((err) => {
const normalizedErrorResult = normalizeErrorResult(err);
return normalizedErrorResult;
})
}
函数doPostRequest 返回一个承诺。我怎样才能伪造这个承诺的返回值,以便我可以断言 normalizeSucessResult 是否已被调用?
所以我试过了:
const normalizeSucessResult = require('./normalizer.js');
const doPostRequest = require('./doPostRequests.js');
const doPost = require('./doPost.js');
it('runs a happy flow scenario', async () => {
let normalizeSucessResultStub = sinon.stub(normalizeSucessResult, 'normalizeSucessResult');
let postData = { body: 'Lorum ipsum' };
let params = { host: 'someUrl', port: 433, method: 'POST', path: '/' };
sinon.stub(doPostRequest, 'doPostRequest').resolves("some response data"); //Fake response from doPostRequest
return doPost.doPost(params, postData).then((res) => { //res should be equal to some response data
expect(normalizeSucessResultStub).to.have.been.calledOnce;
expect(normalizeSucessResultStub).to.have.been.with("some response data");
});
});
doPostRequest 模块如下所示:
const https = require('https')
module.exports.doPostRequest = function (params, postData) {
return new Promise((resolve, reject) => {
const req = https.request(params, (res) => {
let body = []
res.on('data', (chunk) => {
body.push(chunk)
})
res.on('end', () => {
try {
body = JSON.parse(Buffer.concat(body).toString())
} catch (e) {
reject(e)
}
resolve(body)
})
})
req.on('error', (err) => {
reject(err)
})
if (postData) {
req.write(JSON.stringify(postData))
}
req.end()
})
}
【问题讨论】:
-
doPostRequest 在哪里定义?请发布所有相关代码。
-
doPostRequest模块在单独的文件中定义。我已经更新了代码以显示它的位置。如果您对如何解决此问题有任何想法,请分享。我越来越困惑... -
我无法提供详细的答案,但是您需要模拟从中导出 doPostRequest 的模块(使用 Proxyquire 或类似的东西),并在导入第一次使用它的模块之前执行此操作(即doPost)模块。你不能用诗乃做到这一点。您只能使用 Sinon 模拟现有方法,而 doPostRequest 用作函数,而不是方法。顺便说一句,Jest 本地处理模块模拟,您可以通过切换到它来更轻松地做到这一点。
标签: javascript node.js unit-testing mocking sinon