dpikoto468637 2018-09-26 20:51
浏览 73
已采纳

在Go中测试Lambda处理程序时如何模拟AWS Lambda上下文?

I have an S3-triggered AWS Lambda written in Go. I've been able to successfully test all of the ancillary code, however, I'm stuck trying to test the lambda handler.

Here's the signature of my handler:

func HandleRequest(ctx context.Context, s3Event events.S3Event)

Here's the test code:

package main

import (
  "context"
  "encoding/json"
  "testing"

  "github.com/aws/aws-lambda-go/events"
  "github.com/stretchr/testify/assert"
)

func TestHandleRequest(t *testing.T) {
  // 1. read JSON from file
  inputJSON, err := readJSONFromFile("./testdata/s3-event.json")
  if err != nil {
    t.Errorf("could not open test file. details: %v", err)
  }

  // 2. de-serialize into Go object
  var inputEvent events.S3Event
  if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
    t.Errorf("could not unmarshal event. details: %v", err)
  }

  // 3. How can I mock the context.Context?

  assert.NoError(t, HandleRequest(context.Context, inputEvent))
}

I have no clue how I should mock the context.Context. I couldn't find any examples online either.

Anyone know? Does my code look idiomatic for testing an S3-triggered, Go Lambda?

  • 写回答

2条回答

      报告相同问题?

      悬赏问题

      • ¥15 用MATLAB解决问题,需要代码
      • ¥45 关于C语言设计井字棋游戏问题的实现
      • ¥15 python定义一个学生类
      • ¥20 vuforia的Unity3D开发,模型附上特效后大量报错
      • ¥35 Python实现文本关键词提取
      • ¥20 如何使用c++语言完成实验仪器管理系统
      • ¥15 请问Unity使用Magica Cloth插件怎么调整散兵的帘子和振袖比较好?
      • ¥15 请问为什么原神模型导入Unity做动画默认姿势会站不直?
      • ¥15 ld: cannot open C:\Program: No such file or directory
      • ¥15 请问汏佬要怎么访问这种过滤了post和get的php内容?