• Stars
    star
    139
  • Rank 262,901 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created about 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ•‹ Mocking Redis in unit tests in Go.

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

More Repositories

1

c2go

βš–οΈ A tool for transpiling C to Go.
Go
1,954
star
2

pie

πŸ• Enjoy a slice! A utility library for dealing with slices and maps that focuses on type safety and performance.
Go
1,655
star
3

orderedmap

πŸ”ƒ An ordered map in Go with amortized O(1) for Set, Get, Delete and Len.
Go
614
star
4

sshtunnel

πŸš‡ Ultra simple SSH tunnelling for Go programs.
Go
253
star
5

vsql

✌️ Single-file or PostgreSQL-server compatible transactional SQL database written in pure V.
V
236
star
6

dingo

🐺 Easy, fast and type-safe dependency injection for Go.
Go
185
star
7

tf

βœ”οΈ tf is a microframework for parameterized testing of functions and HTTP in Go.
Go
136
star
8

pepper

🌢️ Create reactive frontends without ever writing frontend code.
Go
110
star
9

phpserialize

πŸ“‘ PHP serialize() and unserialize() for Go
Go
108
star
10

mbzdb

🎡 Port of the MusicBrainz database to run on other RDBMSs with replication (previously named MB_MySQL.)
Perl
88
star
11

gedcom

πŸ‘ͺ A Go library and CLI tools for encoding, decoding, traversing, merging, comparing, querying and publishing GEDCOM files.
Go
72
star
12

ghost

πŸ‘» Locate and fix overly complex lines of code in Go.
Go
53
star
13

concise

βœ… Concise is test framework for using plain English and minimal code, built on PHPUnit.
PHP
47
star
14

CollectionFactory

🏭 Translation between native collections in Objective-C and serialized formats like JSON.
Objective-C
41
star
15

sqltest

πŸ“ A comprehensive suite of SQL tests for testing the conformance of databases.
Python
39
star
16

bento

🍱 bento is an English-based automation language designed to be used by non-technical people.
Go
32
star
17

redis-usage

πŸ‘οΈ A non-blocking way to count the number of keys or size of Redis key prefixes
Go
30
star
18

vlang-sublime

Sublime Text support for the V programming language
Python
29
star
19

reflect

πŸͺž Runtime reflection for V (vlang)
V
27
star
20

ok

πŸ†— - a strongly-duck-typed language.
Go
17
star
21

mocksqs

πŸ“€ In-memory implementation of SQS ideal for unit testing.
Go
12
star
22

sqlite3x

100% compatible sqlite3 fork with more features
C
10
star
23

gedcompare

Compare GEDCOM files
Python
9
star
24

go-named-params

Named parameters for Go functions
Go
9
star
25

testify-stats

πŸ”’ testify: print test and assertions statistics at the end of the test suite
Go
7
star
26

independentreserve

πŸ’Έ PHP API for independentreserve.com
PHP
6
star
27

tesseract

πŸ”³ tesseract is a SQL object database with Redis as the backend, think of it like a document store that you run SQL statements against.
Python
6
star
28

GoogleMusicClient

🎢 Google Music Client in Objective-C
Objective-C
6
star
29

tui

🎨 Simple Go library for building complex text user interfaces
Go
5
star
30

wikitranslate

Easier translation of Wikipedia pages with CAT tools.
Go
5
star
31

multiselector

Multi Selector for Paw
JavaScript
4
star
32

iterator

Iterator builders for PHP
PHP
4
star
33

postgresql-partitioning

πŸ–– Automatic tools for managing partitions
4
star
34

switch-check

Validate switch statements contain all enum values.
Go
4
star
35

jsonrpc

πŸ’¬ Simple JSON-RPC server for Go
Go
3
star
36

vscode-ok

Language highlighting for the ok programming language in VSCode
2
star
37

tracklist-editor

A tool for editing track lists
JavaScript
2
star
38

sql2mql

β˜• Pure coffeescript SQL to MongoDB statement parser.
CoffeeScript
2
star
39

eagle

πŸ¦… Eagle is a highly parallel column-oriented embedded SQL database engine.
C
2
star
40

chartbrainz

The unofficial way to view charts from MusicBrainz.
Vue
2
star
41

Z

πŸ‡ΏJava to native C compiler.
Java
2
star
42

Hoard

🐿️ A PSR-compliant caching library for holding objects in nested pools with scripting ability.
PHP
2
star
43

Sentinel

Non-blocking Java web server.
Java
2
star
44

dandy

πŸ”¬ The handy, dandy test generation tool for Go.
Go
2
star
45

play.getok.dev

πŸ•ΉοΈ play.getok.dev
HTML
1
star
46

Pluralizer

Objective-C Library/Cocoa Pod for Simple String Pluralization
Objective-C
1
star
47

maven

⚚ maven programming language
C++
1
star
48

csv

Reading and writing CSV files in OK.
1
star
49

delta

πŸ”Ί The fastest scripting language on the planet based on PHP (pre-alpha)
C
1
star
50

construe

Language conversion and maintaining tool.
CoffeeScript
1
star
51

toy

🧸 A toy language
JavaScript
1
star
52

EagleDB

🐦 Concept column-oriented MVCC SQL daemon.
Java
1
star
53

intuit-quickbooks

The PHP SDK for QuickBooks v3 is set of PHP classes that make it easier to call QuickBooks APIs.
PHP
1
star
54

independentreserve-python

Python API for independentreserve.com
1
star
55

codekata.io

Online and realtime Kata for TDD
PHP
1
star
56

rateyourmusic-todo

Discussion and queue for entities that need editing help
1
star