如何从mountebank响应块中的另一个js文件调用函数



我有一个关于mountebank响应装饰的问题。我未能从响应代码块中的另一个js文件中调用函数。有人能给我一个提示吗?

我的js文件:utils.js

function getRandomCharAndNum(min, max){
let returnStr = "";
const range = (max ? Math.round(Math.random() * (max-min)) + min : min);
const charStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for(let i=0; i<range; i++){
const index = Math.round(Math.random() * (charStr.length-1));
returnStr = returnStr + charStr.substring(index,index+1);
}
return returnStr;
}

mountebank中的响应代码是

const utils = require('./utils/utils');
responses: [{
is: {
statusCode: 200,
headers: {
"Consent-Type": "application/json"
}
},
_behaviors: {
decorate: `(config) => {
config.response.body = { 
id: utils.getRandomCharAndNum()
};
}`
}
}]

错误为:{"errors":[{"message":"Converting circular structure to JSON","name":"TypeError","stack":"TypeError: Converting circular structure to JSONn at JSON.stringify (<anonymous>)n at responsePromise.then.response (/Users/xinxing.cheng/Documents/app/node_modules/mountebank/src/models/behaviors.js:251:48)n at _fulfilled (/Users/xinxing.cheng/Documents/app/node_modules/q/q.js:854:54)n at /Users/xinxing.cheng/Documents/app/node_modules/q/q.js:883:30n at Promise.promise.promiseDispatch (/Users/xinxing.cheng/Documents/app/node_modules/q/q.js:816:13)n at /Users/xinxing.cheng/Documents/app/node_modules/q/q.js:877:14n at runSingle (/Users/xinxing.cheng/Documents/app/node_modules/q/q.js:137:13)n at flush (/Users/xinxing.cheng/Documents/app/node_modules/q/q.js:125:13)n at process._tickCallback (internal/process/next_tick.js:61:11)"}]}

您必须将getRandomCharAndNum函数内联到装饰函数中,或者直接将其包含在装饰函数中。看起来您有一些调用mountebank的包装器(未显示(,但不能在decorator之外包含相关的helper函数。

相关内容

  • 没有找到相关文章

最新更新