Unit Test (Redis) in Golang
By: Bismo Baruno
Tags: Mock, Redis, Unit Testing, Go, Golang

Unit Test (Redis) in Golang

How to mock our Redis code in Golang for Unit Test

Hi everyone! With me, again and again, to share some experience, thought, or opinion about technology-related with the software engineering field. In this part, I want to share how to mock our Redis code in Golang for Unit Test purposes.

Honestly, I am a little bit confused about how to make it for Redis. Because there’s no popular library like go-sqlmock from DATA-DOG. Until I found this great article, “Mocking Redis In Unit Tests In Go”.

Nice, it’s very helpful! But, I should modify my existing code for works well both on the test and real implementation. So, let’s start making a simple example test! We will need 3 libraries to run the test.

For the fake Redis server: https://github.com/alicebob/miniredis

https://github.com/alicebob/miniredis For interact with Redis: shttps://github.com/elliotchance/redismock

shttps://github.com/elliotchance/redismock For the Redis client: https://github.com/go-redis/redis

First, create the project structure like this:

+ your_gopath/

|

+--+ src/github.com/moemoe89

| |

| +--+ go-unit-test-redis/

| |

| +--+ main.go

| + repository/

| |

| +--+ repository.go

| + repository_test.go

|

+--+ bin/

| |

| +-- ... executable file

|

+--+ pkg/

|

+-- ... all dependency_library required

After that, we will need to make 2 files: repository.go and repository_test.go under repository directory.

First, repository.go file will have the implementation of Repository interface which has 2 simple functions for Set and Get data. If you are already familiar with the clean architecture concept and dependency injection, that code is easy for you. If you are not familiar with it, I will recommend this article Trying Clean Architecture on Golang by Iman Tumorang. It is a great article, also exists in Bahasa Indonesia, Mencoba Clean Architecture pada Golang. Thanks to Iman for your sharing.

Last, the test file repository_test.go will be testing the repository code using Mock data. I will explain more on this part for better understanding.

The main code for this test, we need to run the fake Redis server using Run function from miniredis.

mr, err := miniredis.Run()

if err != nil {

log.Fatalf("an error '%s' was not expected when opening a stub database connection", err)

}

Then, create the client connection from the go-redis library with the address from miniredis address.

client = redis.NewClient(&redis.Options{

Addr: mr.Addr(),

})

Next, we can create the mock function using redismock library from elliotchance. In this code below, it’s the example for mock data from our Set and Get function.

mock := redismock.NewNiceMock(client)

mock.On("Set", key, val, exp).Return(redis.NewStatusResult("", nil)) mock := redismock.NewNiceMock(client)

mock.On("Get", key).Return(redis.NewStringResult(val, nil))

If you already using go-sqlmock from DATA-DOG, is it similar, right?

Okay! It’s time to run the test. Use go test ./... for executing the test cases.

Unit Test Result

And …. Cool, we have passed the test!! It’s working!!

If you want to see the full example project for this test, you can visit my repository for this project on GitHub, moemoe89/go-unit-test-redis.

Hope you enjoy it, I’m happy if this article useful for you! Happy testing!

Thank you!