Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock Test on SQS SendMessage Example #5011

Closed
1 of 2 tasks
Shokoufehsafa opened this issue Oct 5, 2023 · 2 comments
Closed
1 of 2 tasks

Mock Test on SQS SendMessage Example #5011

Shokoufehsafa opened this issue Oct 5, 2023 · 2 comments
Assignees
Labels
guidance Question that needs advice or information.

Comments

@Shokoufehsafa
Copy link

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

@Shokoufehsafa Shokoufehsafa added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Oct 5, 2023
@lucix-aws lucix-aws added guidance Question that needs advice or information. and removed feature-request A feature should be added or improved. labels Oct 6, 2023
@RanVaknin RanVaknin self-assigned this Oct 10, 2023
@RanVaknin RanVaknin removed the needs-triage This issue or PR still needs to be triaged. label Oct 10, 2023
@RanVaknin
Copy link
Contributor

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~

@github-actions
Copy link

⚠️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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

3 participants