All Projects → elliotchance → Redismock

elliotchance / Redismock

Licence: mit
🕋 Mocking Redis in unit tests in Go.

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Redismock

Ioredis Mock
Emulates ioredis by performing all operations in-memory.
Stars: ✭ 181 (+82.83%)
Mutual labels:  redis, mocking
mock-spy-module-import
JavaScript import/require module testing do's and don'ts with Jest
Stars: ✭ 40 (-59.6%)
Mutual labels:  stub, mocking
Spy
Clojure/ClojureScript library for stubs, spies and mocks.
Stars: ✭ 131 (+32.32%)
Mutual labels:  mocking, stub
Mockery
Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succinct API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL).
Stars: ✭ 10,048 (+10049.49%)
Mutual labels:  mocking, stub
Cuckoo
Boilerplate-free mocking framework for Swift!
Stars: ✭ 1,344 (+1257.58%)
Mutual labels:  mocking, stub
node-muk
Mock object methods and dependencies.
Stars: ✭ 57 (-42.42%)
Mutual labels:  stub, mocking
aem-stubs
Tool for providing sample data for AEM applications in a simple and flexible way. Stubbing server on AEM, no separate needed.
Stars: ✭ 40 (-59.6%)
Mutual labels:  stub, mocking
Clj Fakes
An isolation framework for Clojure/ClojureScript.
Stars: ✭ 26 (-73.74%)
Mutual labels:  mocking, stub
Ohhttpstubs
Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!
Stars: ✭ 4,831 (+4779.8%)
Mutual labels:  mocking, stub
Hippolyte
HTTP Stubbing in Swift
Stars: ✭ 109 (+10.1%)
Mutual labels:  stub, mocking
Ts Mockito
Mocking library for TypeScript
Stars: ✭ 608 (+514.14%)
Mutual labels:  mocking, stub
Wiremockui
Wiremock UI - Tool for creating mock servers, proxies servers and proxies servers with the option to save the data traffic from an existing API or Site.
Stars: ✭ 38 (-61.62%)
Mutual labels:  mocking, stub
Django Redis Metrics
Metrics for django apps backed by Redis.
Stars: ✭ 93 (-6.06%)
Mutual labels:  redis
Proxy
📡 Redsmin proxy deamon - Access local redis instance from Redsmin
Stars: ✭ 96 (-3.03%)
Mutual labels:  redis
Seckill
基于SpringMVC,Spring,MyBatis实现的秒杀系统(参见慕课网,做了些改动)
Stars: ✭ 93 (-6.06%)
Mutual labels:  redis
Capture Thread
Lock-free framework for loggers, tracers, and mockers in multithreaded C++ programs.
Stars: ✭ 93 (-6.06%)
Mutual labels:  mocking
Raf Stub
Accurate and predictable testing of requestAnimationFrame and cancelAnimationFrame
Stars: ✭ 97 (-2.02%)
Mutual labels:  stub
Sacrificial Socket
A Go websocket library with an API similar to Socket.IO... but not Socket.IO
Stars: ✭ 96 (-3.03%)
Mutual labels:  redis
Zhihuspider
知乎用户公开个人信息爬虫, 能够爬取用户关注关系,基于Python、使用代理、多线程
Stars: ✭ 92 (-7.07%)
Mutual labels:  redis
Aws Lambda Vpc Nat Examples
Example of setting up AWS lambda function with VPC and NAT
Stars: ✭ 92 (-7.07%)
Mutual labels:  redis

redismock

Package github.com/elliotchance/redismock is useful for unit testing applications that interact with Redis. It uses the stretchr/testify mocking system.

Unlike using a real or fake Redis (more below), redismock provides normal and nice mocks for more flexibility in control behavior. One example might be to test the case where Redis takes too long to respond to a command, or returns an expected error.

NewMock()

Creates a hollow mock. You will need to stub all commands that you will interact with.

This is most useful when you want to strictly test all Redis interactions.

NewNiceMock(client)

Create a mocks that falls back to the real client in cases where a command has not been stubbed.

This is most useful when you want a real Redis instance, but you need to stub off certain commands or behaviors.

Compatibility

github.com/go-redis/redis

go-redis/redis is a real Redis client, and one of the most popular. It expects to connect to a real Redis server.

It provides a huge interface for all of the Redis commands called Cmdable. You should use Cmdable interface throughout your code, instead of the struct type.

github.com/alicebob/miniredis

miniredis creates a fake Redis server that can be used by go-redis/redis which makes it fantastic for running unit tests since you do not need to run a separate Redis server. It also only retains state with the instance, so each test gets an empty Redis database each time.

Example

import (
	"testing"
	"github.com/go-redis/redis"
	"github.com/alicebob/miniredis/v2"
	"github.com/elliotchance/redismock"
	"errors"
	"github.com/stretchr/testify/assert"
)

// newTestRedis returns a redis.Cmdable.
func newTestRedis() *redismock.ClientMock {
	mr, err := miniredis.Run()
	if err != nil {
		panic(err)
	}

	client := redis.NewClient(&redis.Options{
		Addr: mr.Addr(),
	})

	return redismock.NewNiceMock(client)
}

// This would be your production code.
func RedisIsAvailable(client redis.Cmdable) bool {
	return client.Ping().Err() == nil
}

// Test Redis is down.
func TestRedisCannotBePinged(t *testing.T) {
	r := newTestRedis()
	r.On("Ping").
		Return(redis.NewStatusResult("", errors.New("server not available")))

	assert.False(t, RedisIsAvailable(r))
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].