I want to local test lambda function call and not use aws sns service.
func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error){
...do some logic and db opeations.
err := db.Create(something)
if err != nil {
msg := "something error."
target := "sns:arn"
// do sns notification
sess := session.Must(session.NewSession())
svc := sns.New(sess,&cfg)
err = PublishNotification(svc, msg, target)
}}
func PublishNotification(svc *sns.SNS, message string, target string) error {
params := sns.PublishInput{
Message: &message,
Subject: &subject,
TargetArn: &target,
}
req, _ := svc.PublishRequest(¶ms)
err := req.Send()
if err != nil {
return err
}
return nil
}
func main(){
lambda.Start(Handler)
}
Now Everything works fine. And I want to unit test or integration test this lambda handler.
func TestLabmdaWithSns(t *testing.T) {
tests := []struct {
request events.APIGatewayProxyRequest
}{
request: events.APIGatewayProxyRequest{Body:"something"}
}
for _, test := range tests {
response, err := Handler(test.request)
// assert something
}
}
And I dont want to send sns notification when testing.
How should I mock this svc or PublishNotification function to not publish message to sns when local testing ?
Thanks.
You should use interface snsiface.SNSAPI instead of *sns.SNS in PublishNotification. Then you can inject mock implementation of snsiface.SNSAPI to the unit test as described here: https://docs.aws.amazon.com/sdk-for-go/api/service/sns/snsiface/