Golang Testing — Mocking Redis. I recently discovered how to mock Redis in my testing environments with little friction. When testing our applications we often have a lot of external dependencies, such as Redis. When testing we should try to mock this.
When testing our applications we often have a lot of external dependencies, and often we’re not running our tests in an environment where we have room to boot up Redis, MySQL etc.
Recently I was trying to solve the problem of needing a Redis connection in one of my applications, but I didn’t want to have to keep a Redis server running whilst I ran my tests, I felt this was unnecessary.
I stumbled upon an elegant solution to this: https://github.com/alicebob/miniredis
This library spins up a Redis compatible server in your application that you can use without dependencies when running your tests in any environment. This is perfect!
Say in your application you’re making use of a
redisClient which is available globally in your package scope:
In this case we’re using `github.com/go-redis/redis_` as our Redis library of choice._
What do you do when you have lots of free time on your hands? Why not learn test programming strategies and approaches?
Redis Lua scripting is the popularly recommended approach for handling transactions. Learn the common Lua Scripts error and how to handle for sentinel systems. Redis offers two mechanisms for handling transactions – MULTI/EXEC based transactions and Lua scripts evaluation. Redis Lua scripting is the recommended approach and is fairly popular in usage.
API endpoint when you send the get request to that URL it returns the JSON response. In this article, I am going to use postman assertions for all the examples since it is the most popular tool. But this article is not intended only for the postman tool.
Let’s step up our testing game with two useful libraries that lend themselves excellently to a TDD approach.
The shift towards microservices and modular applications makes testing more important and more challenging at the same time. Learn more here.