Giter VIP home page Giter VIP logo

redismock's Introduction

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"
	"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))
}

redismock's People

Contributors

elliotchance avatar ltpquang avatar mjcbsn22 avatar supernan1994 avatar nmakro avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.