AWS golang lambdaiface 用于测试 Lambda 的使用方法

huangapple go评论73阅读模式
英文:

AWS golang lambdaiface usage for testing lambda

问题

我正在尝试对一个 Lambda 函数进行单元测试。根据文档(https://docs.aws.amazon.com/sdk-for-go/api/service/lambda/lambdaiface)的说明,我不确定如何实现。给出的示例是无法工作的,我也没有看到其他用法。对我来说特别困惑的是:

func myFunc(svc lambdaiface.LambdaAPI) bool {
    // Make svc.AddLayerVersionPermission request
}

因为 AddLayerVersionPermission 并不返回 bool 值,所以我对预期的用法感到困惑。我也不确定是否需要逐个模拟每个函数,这将很繁琐。有人可以给我一些指导或有效的示例吗?最终,我只想模拟 Invoke() 函数。

我尝试阅读文档,但对我来说不太清楚。

英文:

I'm trying to unit test a lambda function. Looking at the docs (https://docs.aws.amazon.com/sdk-for-go/api/service/lambda/lambdaiface) I'm not sure how to achieve it. The example given is non working and I cannot see any other usage of it. Particularly confusing to me is

func myFunc(svc lambdaiface.LambdaAPI) bool {
    // Make svc.AddLayerVersionPermission request
}

as AddLayerVersionPermission does not return a bool so again I'm confused with expected usage. I'm also unsure if I'm meant to mock out every function individually which would be tedious. Can anyone give me any pointers or working examples? Ultimately I only want to mock out Invoke()

Thanks

I tried reading the documentation but it was unclear to me.

答案1

得分: 1

文档中的myFunc()返回一个不必要的布尔值,这让我对它的工作原理感到困惑。我的同事https://stackoverflow.com/users/966860/nathj07帮助我解决了这个问题。以下是他帮助我得到的完整解决方案:

package main

import (
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/lambda"
	"github.com/aws/aws-sdk-go/service/lambda/lambdaiface"
	"github.com/stretchr/testify/mock"
)

type MockLambdaClient struct {
	mock.Mock
	lambdaiface.LambdaAPI
}

func (mlc *MockLambdaClient) Invoke(input *lambda.InvokeInput) (*lambda.InvokeOutput, error) {
	args := mlc.Called(input)
	return args.Get(0).(*lambda.InvokeOutput), args.Error(1)
}

type app struct {
	lambdaClient lambdaiface.LambdaAPI
}

func (a *app) invokeTest() {
	_, err := a.lambdaClient.Invoke(&lambda.InvokeInput{
		FunctionName: aws.String("a func name"),
		Payload:      []byte("the bytes"),
	})
	if err != nil {
		panic(err)
	}
}

func main() {

}

以及测试代码:

package main

import (
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/lambda"
)

func TestExample(t *testing.T) {
	mockLambda := &MockLambdaClient{}
	defer mockLambda.AssertExpectations(t)

	expInput := &lambda.InvokeInput{
		FunctionName: aws.String("a func name"),
		Payload:      []byte("the bytes"),
	}
	mockLambda.On("Invoke", expInput).Return(&lambda.InvokeOutput{}, nil).Times(1)

	a := app{}
	a.lambdaClient = mockLambda
	a.invokeTest()
}

以上是你要翻译的内容。

英文:

That the documentations has a unneccessary boolean return from myFunc() threw me on how this worked. A colleague https://stackoverflow.com/users/966860/nathj07 got me through this. This is the full solution he helped me get

package main

import (
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/lambda"
	"github.com/aws/aws-sdk-go/service/lambda/lambdaiface"
	"github.com/stretchr/testify/mock"
)

type MockLambdaClient struct {
	mock.Mock
	lambdaiface.LambdaAPI
}

func (mlc *MockLambdaClient) Invoke(input *lambda.InvokeInput) (*lambda.InvokeOutput, error) {
	args := mlc.Called(input)
	return args.Get(0).(*lambda.InvokeOutput), args.Error(1)
}

type app struct {
	lambdaClient lambdaiface.LambdaAPI
}

func (a *app) invokeTest() {
	_, err := a.lambdaClient.Invoke(&lambda.InvokeInput{
		FunctionName: aws.String("a func name"),
		Payload:      []byte("the bytes"),
	})
	if err != nil {
		panic(err)
	}
}

func main() {

}

and the tests

package main

import (
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/lambda"
)

func TestExample(t *testing.T) {
	mockLambda := &MockLambdaClient{}
	defer mockLambda.AssertExpectations(t)

	expInput := &lambda.InvokeInput{
		FunctionName: aws.String("a func name"),
		Payload:      []byte("the bytes"),
	}
	mockLambda.On("Invoke", expInput).Return(&lambda.InvokeOutput{}, nil).Times(1)

	a := app{}
	a.lambdaClient = mockLambda
	a.invokeTest()
}

huangapple
  • 本文由 发表于 2022年4月6日 18:08:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/71764819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定