Comments (4)
Hi @prvn,
The redis package is not designed to be mockable
, if you want to mock it you will need to create your own mockable
wrapper.
Here you have an example:
package mock
import (
"errors"
"testing"
"gopkg.in/redis.v3"
)
type Client struct {
base redis.Client
}
func (c *Client) ZAdd(key string, members ...redis.Z) IntCmdInterface {
return c.base.ZAdd(key, members...)
}
func (c *Client) ZRem(key string, members ...string) IntCmdInterface {
return c.base.ZRem(key, members...)
}
type ClientInterface interface {
ZAdd(key string, members ...redis.Z) IntCmdInterface
ZRem(key string, members ...string) IntCmdInterface
}
type ClientMock struct {
Mock
}
func (m *ClientMock) ZAdd(key string, members ...redis.Z) IntCmdInterface {
ret := m.Called(key, members)
return ret.Get(0).(IntCmdInterface)
}
func (m *ClientMock) ZRem(key string, members ...string) IntCmdInterface {
ret := m.Called(key, members)
return ret.Get(0).(IntCmdInterface)
}
type IntCmdInterface interface {
Result() (int64, error)
}
type IntCmdMock struct {
Mock
}
func (m *IntCmdMock) Result() (int64, error) {
ret := m.Called()
return ret.Int64(0), ret.Error(1)
}
func TestRedis(t *testing.T) {
client := ClientMock{}
cmd1 := &IntCmdMock{}
cmd2 := &IntCmdMock{}
cmd3 := &IntCmdMock{}
cmd1.When("Result").Return(int64(123), nil)
cmd2.When("Result").Return(int64(456), nil)
cmd3.When("Result").Return(int64(0), errors.New("an error"))
client.When("ZAdd", "foo", Any).Return(cmd1)
client.When("ZRem", "bar", Any).Return(cmd2)
client.When("ZRem", "zar", Any).Return(cmd3)
ret := client.ZAdd("foo", redis.Z{1.00, "member"})
if i, err := ret.Result(); i != 123 || err != nil {
t.Fatal("Result is not 123")
}
ret = client.ZRem("bar")
if i, err := ret.Result(); i != 456 || err != nil {
t.Fatal("Result is not 456")
}
ret = client.ZRem("zar")
if i, err := ret.Result(); i != 0 || err == nil || err.Error() != "an error" {
t.Fatal("error is not `an error`")
}
}
from go-mock.
That was super helpful! Thanks @maraino
from go-mock.
@prvn And of course as you're doing your own wrapper you can simplify the mock if you do things like:
func (c *Client) ZAdd(key string, members ...redis.Z) (int64, error) {
return c.base.ZAdd(key, members...).Result()
}
from go-mock.
I was thinking about the same, so that I can avoid mocking all the Result() functions of each Cmd structs.
from go-mock.
Related Issues (8)
- When using MockFunction.Call, only the first set of return values is returned HOT 1
- what if the function return a struct, how can i mock it? HOT 1
- Please add Go module HOT 3
- Mock not working HOT 1
- Support variable number of arguments HOT 6
- how to walk over the limits: need use obj as a param to control if it's a mock obj HOT 6
- Feature Request: Given an interface, generate a mock HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-mock.