redigomock

package module
v0.0.0-...-522603d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

README

What's it?

When you use redis in your project, How to mock it is the best problem to UT. redisgomock is one library for mock redis.

Why write by myself?

redigo recommend rafaeljusto/redigomock. But I think it can be better:

  • Use Interface, not struct
  • Fuzzy Match can be simple

In fact, I want submit pull request to rafaeljusto/redigomock, but will change so much, So I write one by myself

How to Use

conn, mock := NewMockClient()
// prepare 
mock.ExpectDo("SADD").WillReply(int64(1))
mock.ExpectClose()

// do
count, err := redis.Int64(conn.Do("SADD", "element"))
conn.Close()

// assert
if count != 1 {
    ....
}

TODO

  • UT Coverage > 70%
  • design doc

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action interface {
	// contains filtered or unexported methods
}

type Config

type Config struct {
	Order      bool
	FuzzyMatch bool
}

type ErrRspStub

type ErrRspStub interface {
	WillReturnError(err error)
	// contains filtered or unexported methods
}

type Expection

type Expection struct {
	// contains filtered or unexported fields
}

type Matcher

type Matcher interface {
	Match(operation *Operation) (Action, error)
}

type Operation

type Operation struct {
	Opt  string
	Cmd  string
	Args []interface{}
}

func (*Operation) Equal

func (opt *Operation) Equal(b *Operation, config *Config) bool

func (*Operation) String

func (opt *Operation) String() string

type Option

type Option func(*Config)

type RedigoMock

type RedigoMock interface {
	ExpectationsWereMet() error

	// Close closes the connection.
	ExpectClose() ErrRspStub // error

	// Err returns a non-nil value when the connection is not usable.
	ExpectInvokeErr() ErrRspStub // error

	// Do sends a command to the server and returns the received reply.
	ExpectDo(commandName string, args ...interface{}) ReplyRspStub // (reply interface{}, err error)

	// Send writes the command to the client's output buffer.
	ExpectSend(commandName string, args ...interface{}) ErrRspStub // error

	// Flush flushes the output buffer to the Redis server.
	ExpectFlush() ErrRspStub

	// Receive receives a single reply from the Redis server
	ExpectReceive() ReplyRspStub // (reply interface{}, err error)
}

func New

func New(opts ...Option) (redis.Conn, RedigoMock)

New return one conn and mock

type ReplyRspStub

type ReplyRspStub interface {
	ErrRspStub
	WillReply(reply interface{})
}

type Value

type Value struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL