0

我有一个关于 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 JSON\n 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:30\n 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:14\n 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)"}]}

4

1 回答 1

1

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

于 2019-11-17T22:23:54.890 回答