1

我试图调用同一个 ejs 文件中的另一个函数。

async function (request, state, logger) {
    var fs = require('fs');
    var path = require('path');
    var util = require('util');
    var response;
    var RspFilename = "my-success.xml";
    var responsepath = path.join('ConfigFiles', 'Responses', 'ABS', RspFilename);
    response = fs.readFileSync((responsepath), 'utf8');

    await this.sleep(1000);
    return {
        headers: {
            "HTTP-Response-Code": 200,
            "HTTP-Response-Code-Text": "OK",
            "Content-Type": "text/xml"
        },
        "statusCode": "200",
        body: response
    };
}


function sleep(ms) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
} 

我正在尝试这个mountebank。调用端点时,我得到以下错误

{
    "errors": [
        {
            "code": "invalid injection",
            "message": "invalid response injection",
            "source": "(async function (request, state, logger) {\n    var fs = require('fs');\n    var path = require('path');\n    var util = require('util');\n    var response;\n    var RspFilename = \"my-success.xml\";\n    var responsepath = path.join('ConfigFiles', 'Responses', 'ABS', RspFilename);\n    response = fs.readFileSync((responsepath), 'utf8');\n\n    logger.info(\"---------Response Sent Successfully--------\" + RspFilename + response);\n    await this.sleep(1000);\n    return {\n        headers: {\n            \"HTTP-Response-Code\": 200,\n            \"HTTP-Response-Code-Text\": \"OK\",\n            \"Content-Type\": \"text/xml\"\n        },\n        \"statusCode\": \"200\",\n        body: response\n    };\n}\n\n\nfunction sleep(ms) {\n    return new Promise((resolve) => {\n        setTimeout(resolve, ms);\n    });\n})(scope, injectState, logger, deferred.resolve, imposterState);",
            "data": "Unexpected token function",
            "name": "Error",
            "stack": "Error\n    at Object.from (C:\\Repo\\mountebank\\src\\util\\inherit.js:15:17)\n    at createError (C:\\Repo\\mountebank\\src\\util\\errors.js:10:26)\n    at Object.InjectionError (C:\\Repo\\mountebank\\src\\util\\errors.js:25:16)\n    at inject (C:\\Repo\\mountebank\\src\\models\\responseResolver.js:43:44)\n    at processResponse (C:\\Repo\\mountebank\\src\\models\\responseResolver.js:243:20)\n    at Object.resolve (C:\\Repo\\mountebank\\src\\models\\responseResolver.js:277:16)\n    at Object.resolve (C:\\Repo\\mountebank\\src\\models\\stubRepository.js:136:25)\n    at F.respond (C:\\Repo\\mountebank\\src\\models\\http\\baseHttpServer.js:94:34)\n    at C:\\Repo\\mountebank\\src\\models\\abstractServer.js:105:39\n    at _fulfilled (C:\\Repo\\mountebank\\node_modules\\q\\q.js:854:54)"
        }
    ]
}

实际上我的要求是为响应增加一些延迟。

顺便说一句,我尝试了 Mountebank wait,但看不到任何延迟。

4

2 回答 2

1

按照http://www.mbtest.org/docs/api/injection上的代码示例,mountebank 似乎不希望任何东西跟随单个匿名函数,因此当它开始读取时会出现“意外的令牌函数” function sleep

尝试sleep进入其他功能。不远了,因为您已经对模块导入做了同样的事情:

async function (request, state, logger) {
    // ...
    function sleep(ms) {
        return new Promise((resolve) => {
            setTimeout(resolve, ms);
        });
    } 

    await sleep(1000);
    // ...
}

或者干脆

async function (request, state, logger) {
    // ...
    await new Promise(resolve => setTimeout(resolve, 1000));
    // ...
}

或者,简化整个功能

function (request, state, logger) {
    // ...
    return new Promise(resolve => setTimeout(() => {
        resolve({
            headers: // ...

        })
    }, 1000);
}
于 2021-11-24T16:14:43.077 回答
0

您正在声明一个函数,但忘记在第 1 行给它命名。

如果您打算定义一个匿名函数而不是创建一个async function 语句,则需要将其括在括号中。

于 2021-11-24T12:27:23.623 回答