Categorygithub.com/arhea/go-mock-redis
modulepackage
1.0.1
Repository: https://github.com/arhea/go-mock-redis.git
Documentation: pkg.go.dev

# README

Mock Redis

Tests goreportcard

Provide a mock Redis instance and optionally a mock Redis client for testing purposes. This library is built so you can mock Redis instances using real Redis containers. You will need to have Docker running on your local machine or within your CI environment.

This library is built on top of testcontainers.

Usage

Creating a mock instance for creating a customer connection.

func TestXXX(t *testing.T) {
	ctx := context.Background()

	mock, err := mockredis.NewInstance(ctx, t)

	if err != nil {
		t.Fatalf("creating the instance: %v", err)
		return
	}

	// close the mock
	defer mock.Close(ctx)

	// ... my test code
}

Creating a mock redis client for interacting with Redis.

func TestXXX(t *testing.T) {
	ctx := context.Background()

	mock, err := mockredis.NewClient(ctx, t)

	if err != nil {
		t.Fatalf("creating the client: %v", err)
		return
	}

	// close the mock
	defer mock.Close(ctx)

    redisClient := mock.Client()

	// ... my test code
}

# Functions

NewClient creates a new Redis client that connects to an underlying instance for testing.
NewClientWithConfig creates a new Redis client that connects to an underlying instance for testing.
NewInstance creates a new Redis container.

# Structs

Client wraps the Redis client and the underlying instance for testing.
Instance represents the underlying container that is running the mock redis instance.