Git Product home page Git Product logo

Comments (4)

maraino avatar maraino commented on July 28, 2024

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.

prvn avatar prvn commented on July 28, 2024

That was super helpful! Thanks @maraino

from go-mock.

maraino avatar maraino commented on July 28, 2024

@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.

prvn avatar prvn commented on July 28, 2024

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)

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.