node.js - 使用 Vandium : Expected event type of apigateway but identified as unknown 时无服务器调用测试返回状态 500

标签 node.js testing lambda serverless-framework

运行 serverless invoke test 并使用 Vandium 时,我收到 Status Code 500 和错误:

{"type":"Error","message":"Expected event type of apigateway but identified as unknown”}

这是重现问题的代码。首先,工作示例(没有 Vandium):

time/handler.js:

'use strict';

module.exports.endpoint =

(event, context, callback) => {
    const response = {
        statusCode: 200,
        body: JSON.stringify({
            message: `Hello, the current time is ${new Date().toTimeString()}.`,
        }),
    };

    callback(null, response);
}

serverless.yml:

service: serverless-starter

provider:
  name: aws
  runtime: nodejs6.10
functions:
  currentTime:
    handler: time/handler.endpoint
    events:
      - http:
          path: ping
          method: get
plugins:
  - serverless-offline
  - serverless-mocha-plugin

最后,自动生成检查状态码的测试代码: 测试/currentTime.js:

'use strict';

// tests for currentTime
// Generated by serverless-mocha-plugin

const mochaPlugin = require('serverless-mocha-plugin');
const expect = mochaPlugin.chai.expect;
let wrapped = mochaPlugin.getWrapper('currentTime', '/time/handler.js', 'endpoint');

describe('currentTime', () => {
  before((done) => {
    done();
  });

  it('implement tests here', () => {
    return wrapped.run({}).then((response) => {
        const body = JSON.parse(response.body);
        expect(response.statusCode).to.be.equal(200);
    });
  });
});

这很好用。我可以启动服务器:serverless offline start 并且我能够获得 http://localhost:3000/ping端点可以用 curl 或 POSTMAN 处理。

如果我运行 serverless invoke test 那么测试运行正常。到目前为止没有问题。

现在,当我重构代码并使用 Vandium 时: time/handler.js:

'use strict';

const vandium = require('vandium');

module.exports.endpoint = vandium.api()
        .GET((event) => {
            return {
                "id": "12345",
                "name": "john.doe"
            };
        }
    )

然后一切正常,即我可以启动服务器并使用 curl 或 POSTMAN 连接到端点。

但是,当我再次运行测试时 serverless invoke test 然后我得到 500 和错误,而不是状态代码 200:

{"type":"Error","message":"expected event type of apigateway but identified as unknown”}

怎么了?

最佳答案

使用 APIGatewayEvent 对象或支持的 aws 事件之一创建一个 json 文件。然后使用选项 -p 或 -d 运行无服务器(参见:https://serverless.com/framework/docs/providers/aws/cli-reference/invoke-local#options)

sls invoke local -f hello -p path-to-json.json

看看https://github.com/vandium-io/lambda-event-identifier/blob/master/lib/identify.js了解更多信息

关于node.js - 使用 Vandium : Expected event type of apigateway but identified as unknown 时无服务器调用测试返回状态 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46221827/

相关文章:

node.js - 具有Node和Vue的Docker无法启动正在运行的服务器

testing - 您如何测试您的 Web UI 以查看它是否在不同的浏览器中呈现一致?

angularjs - 如何使用给定的 HTML 元素识别复选框值? angularjs-e2e

javascript - 获取每个地点的 10 条最新评论

javascript - 一旦编辑了命令执行者的命令,机器人如何编辑它的消息?

python - 如何在测试中正确设置相对于运行脚本的路径?

c++ - 带有 unique_ptr 参数回归的 Visual Studio 2012 lambda?

java - 在 Java 中,为什么我不能使用 lambda 作为增强的 for 循环表达式?

javascript - 删除 gulp 脚本中的 lambda 函数

node.js - Passport express /注册 500 未知错误