如何解决使用 rewire 模拟 XML 响应
使用 rewire 为以下代码编写单元测试用例的意图。但是,我对“模拟”项目功能一无所知。非常感谢这方面的任何帮助。代码、测试用例和错误详细信息等详细信息如下。如果您需要更多详细信息,请告诉我
如何在 body.item(0) 函数中模拟项目?
错误:
#account-details-helper
#genApiResponse
Test genApiResponse() success:
**TypeError: body.item is not a function**
at Object.genApiResponse (lib\handlers\utils\account-details-helper.js:37:106)
at Context.<anonymous> (test\pccaccountdetailshelper.test.js:165:13)
// account-details-helper Code:
const genApiResponse = function (pccRes) {
console.error(`PCC Account Search Response in genApiResponse is : ${JSON.stringify(pccRes)}`)
let outres = {}
outres.accountExists = 'No'
let { status,body } = pccRes.data
const pccResponseCode = status
let resXMLRoot = {}
if (Object.keys(pccRes).length !== 0 || pccRes.constructor !== Object ) {
resXMLRoot = body.item(0)
let resXMLErrorBody = (resXMLRoot.getElementsByTagNameNS('*','errorMessageDetail')).item(0)
if (resXMLErrorBody !== null) {
let errorString = (resXMLErrorBody.getElementsByTagNameNS('*','message')).item(0).textContent
const errObj = {
type: 'Provider Error',location: 'My Location',moreinformation: errorString,}
errorrule(errObj,pccResponseCode,'Error')
}
}
let resXMLAccountSearchResultBody = resXMLRoot.getElementsByTagNameNS('*','accountSearchResult')
let resIntegrationKeyComponentBody
if (resXMLAccountSearchResultBody.item(0) !== null) {
resIntegrationKeyComponentBody = resXMLRoot.getElementsByTagNameNS('*','integrationKeyComponent').item(0)
outres.accountNumber = resIntegrationKeyComponentBody.getElementsByTagNameNS('*','value').item(0).textContent
outres.accountExists = 'Yes'
}
return outres
}
module.exports.genApiResponse = genApiResponse
// Test Case
describe('#genApiResponse',function () {
it('Test genApiResponse() success',async function () {
const mymod = rewire('../lib/handlers/utils/account-details-helper')
mymod.__set__({
item() {
return '<?xml version=\"1.0"\ encoding=\"UTF-8"\?> <ns1:accountSearchResultHolder xmlns:ns1=\"http://a/b/ws/v1\"><ns1:searchResultInfo><ns1:message xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/><ns1:searchSuccessIndicator>SUCCESS</ns1:searchSuccessIndicator></ns1:searchResultInfo><ns1:searchResults><ns1:accountSearchResult><ns1:accountKey><ns1:components><ns1:integrationKeyComponent><ns1:name>AccountReference</ns1:name><ns1:value>123456789</ns1:value></ns1:integrationKeyComponent></ns1:components><ns1:name>Sample</ns1:name></ns1:accountKey><ns1:attributes><ns1:dataValue><ns1:stringData><ns1:name>AccReference</ns1:name><ns1:value>987654321</ns1:value></ns1:stringData></ns1:dataValue></ns1:attributes></ns1:accountSearchResult></ns1:searchResults></ns1:accountSearchResultHolder>'
},})
let ctx = {
data: {
headers: { 'tid': 2131323,'sid': 123123213,authorization: 'weqewqe32424' },body: '<?xml version=\"1.0" encoding=\"UTF-8"\?> <ns1:accountSearchResultHolder xmlns:ns1=\"http://a/b/ws/v1\"><ns1:searchResultInfo><ns1:message xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/><ns1:searchSuccessIndicator>SUCCESS</ns1:searchSuccessIndicator></ns1:searchResultInfo><ns1:searchResults><ns1:accountSearchResult><ns1:accountKey><ns1:components><ns1:integrationKeyComponent><ns1:name>AccountReference</ns1:name><ns1:value>123456789</ns1:value></ns1:integrationKeyComponent></ns1:components><ns1:name>Sample</ns1:name></ns1:accountKey><ns1:attributes><ns1:dataValue><ns1:stringData><ns1:name>AccReference</ns1:name><ns1:value>987654321</ns1:value></ns1:stringData></ns1:dataValue></ns1:attributes></ns1:accountSearchResult></ns1:searchResults></ns1:accountSearchResultHolder>',},status: 200,}
mymod.genApiResponse(ctx)
})
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。