aws / aws-sdk-go

AWS SDK for the Go programming language.

Home Page:http://aws.amazon.com/sdk-for-go/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mock Test on SQS SendMessage Example

Shokoufehsafa opened this issue · comments

Describe the feature

Mock test for testing SendMessage on AWS SQS

Use Case

No mock test example for SendMessage, would be great to have one for sending messages to queues.

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

SDK version used

1

Environment details (Version of Go (go version)? OS name and version, etc.)

Go version

Hi @Shokoufehsafa,

While we strive to provide comprehensive examples and guidance, it's impossible for us to create examples for every possible operation due to the vast scope of the SDK. Generally, Go developers are expected to be familiar with mocking techniques and dependency inversion, which are integral parts of testing in Go.

However, we do provide the github.com/aws/aws-sdk-go/service/sqs/sqsiface package, which offers an interface for the SQS API client that can be easily mocked for testing purposes.

To help you get started, here's a basic example that demonstrates how to mock the SendMessage function using this interface:

package main

import (
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/sqs"
	"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
	"github.com/stretchr/testify/assert"
	"testing"
)

type mockSQSClient struct {
	sqsiface.SQSAPI
}

func (m *mockSQSClient) SendMessage(input *sqs.SendMessageInput) (*sqs.SendMessageOutput, error) {
	return &sqs.SendMessageOutput{MessageId: aws.String("mockMessageId")}, nil
}

func TestSendMessage(t *testing.T) {
	mockClient := &mockSQSClient{}

	params := &sqs.SendMessageInput{
		MessageBody: aws.String("test message"),
		QueueUrl:    aws.String("https://sqs.us-west-2.amazonaws.com/123456789012/MyTestQueue"),
	}

	resp, err := SendMessage(mockClient, params)

	assert.NoError(t, err)
	assert.Equal(t, *resp.MessageId, "mockMessageId")
}

Thanks,
Ran~

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.