Skip to content

Testing

Kenton Vizdos edited this page Mar 12, 2025 · 2 revisions

How to Test using TypeQueue

TypeQueue comes with built-in mock implementations that let you test your SQS-dependent code without ever connecting to AWS. In this guide, you'll see examples for testing both dispatching and consuming messages using the non-channel-based mocks.

The best part? ITS SO SIMPLE. Dispatchers & Consumers use a shared interface, meaning your code doesn't need to change at all to get really nice mocks in place.

All of the code has already been integration tested against TestContainers, which takes a HUGE testing lift off of you.

Testing Dispatch Code

The MockDispatcher stores dispatched messages in an internal map, so you can verify that messages are sent correctly and that required fields are properly set.

Example

For this guide, we will test the SendAMessage function from getting started.

Below is a very basic example of unit testing code that ends up dispatching a message.

func TestDispatch(t *testing.T) {
    // Create a dispatcher
    dispatcher := typequeue_mocks.MockDispatcher[*ExampleEvent]{}

    // Dispatch a test message.
    SendAMessage(dispatcher)

    // Verify that the message was added to the map.
    messages, ok := dispatcher.Messages["example-queue-name"]
    assert.True(t, ok, "expected target queue key to exist in Messages map")
    assert.Equal(t, 1, len(messages), "expected exactly one message in the target queue")
}

Have questions? Please open an issue and I'll try to help :)

Testing Consumers

It is recommended to test your provided "Processing Function" only. All of the consumption code has been fully unit and integration tested already.

However, if you are wanting to find examples, check out the unit tests for a mock consumer.

In short, pass a typequeue_mocks.MockConsumer instead of the production consumer.

(if you have suggestions on what else to put here, or have more questions, open an issue / PR :D)

Clone this wiki locally