0

使用 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)
      
    })
  })
        
4

0 回答 0