go - 从包函数返回 Mock

标签 go testify

我是 Go 的新手,我在编写测试时遇到了一些问题,特别是模拟包函数的响应。

我正在为 github.com/go-redis/redis 编写一个包装库。目前它确实只有更好的失败错误,但它会随着 statsd 跟踪进一步扩展,但我离题了......

我创建了以下 go 包

package myredis

import (
    "time"

    "github.com/go-redis/redis"
    errors "github.com/pkg/errors"
)

var newRedisClient = redis.NewClient

// Options - My Redis Connection Options
type Options struct {
    *redis.Options
    DefaultLifetime time.Duration
}

// MyRedis - My Redis Type
type MyRedis struct {
    options Options
    client  *redis.Client
}

// Connect - Connects to the Redis Server. Returns an error on failure
func (r *MyRedis) Connect() error {

    r.client = newRedisClient(&redis.Options{
        Addr:     r.options.Addr,
        Password: r.options.Password,
        DB:       r.options.DB,
    })

    _, err := r.client.Ping().Result()
    if err != nil {
        return errors.Wrap(err, "myredis")
    }

    return nil
}

我的问题是我希望 redis.NewClient 返回一个模拟。这是我写的测试代码,但它不起作用:

package myredis

import (
    "testing"

    "github.com/go-redis/redis"
    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/mock"
)

type redisStatusCmdMock struct {
    mock.Mock
}

func (m *redisStatusCmdMock) Result() (string, error) {
    args := m.Called()
    return args.Get(0).(string), args.Error(1)
}

type redisClientMock struct {
    mock.Mock
}

func (m *redisClientMock) Ping() redis.StatusCmd {
    args := m.Called()
    return args.Get(0).(redis.StatusCmd)
}

func TestConnect(t *testing.T) {
    assert := assert.New(t)

    old := newRedisClient
    defer func() { newRedisClient = old }()

    newRedisClient = func(options *redis.Options) *redis.Client {
        assert.Equal("127.0.0.1:1001", options.Addr)
        assert.Equal("password", options.Password)
        assert.Equal(1, options.DB)

        statusCmdMock := new(redisStatusCmdMock)
        statusCmdMock.On("Result").Return("success", nil)

        clientMock := new(redisClientMock)
        clientMock.On("Ping").Return(statusCmdMock)

        return clientMock
    }

    options := Options{}
    options.Addr = "127.0.0.1:1001"
    options.Password = "password"
    options.DB = 1

    r := MyRedis{options: options}

    result, err := r.Connect()

    assert.Equal("success", result)
    assert.Equal(nil, err)
}

我收到以下错误:不能在返回参数中使用 clientMock(类型 *redisClientMock)作为类型 *redis.Client。我想我读到我需要模拟 redis.Client 的所有功能,以便能够在这种情况下将其用作模拟,但事实真的如此吗?这似乎有点矫枉过正,我应该能够以某种方式做到这一点。我该如何让这个测试工作,或者我是否需要重构我的代码以便更容易编写测试?

最佳答案

redis.Client 是一个struct 类型,在 Go 中,struct 类型不是可模拟的。然而,Go 中的接口(interface)可模拟的,所以您可以做的是定义您自己的“newredisclient”函数,而不是返回一个结构返回一个接口(interface)。由于 Go 中的接口(interface)是隐式满足的,因此您可以定义接口(interface),使其由 redis.Client 开箱即用地实现。

type RedisClient interface {
    Ping() redis.StatusCmd
    // include any other methods that you need to use from redis
}

func NewRedisCliennt(options *redis.Options) RedisClient {
    return redis.NewClient(options)
}

var newRedisClient = NewRedisClient

如果您还想模拟 Ping() 的返回值,则需要做更多的工作。

// First define an interface that will replace the concrete redis.StatusCmd.
type RedisStatusCmd interface {
    Result() (string, error)
    // include any other methods that you need to use from redis.StatusCmd
}

// Have the client interface return the new RedisStatusCmd interface
// instead of the concrete redis.StatusCmd type.
type RedisClient interface {
    Ping() RedisStatusCmd
    // include any other methods that you need to use from redis.Client
}

现在 *redis.Client 不再满足 RedisClient 接口(interface),因为 Ping() 的返回类型> 是不同的。注意,redis.Client.Ping()的结果类型是否满足RedisClient.Ping()返回的接口(interface)类型并不重要,重要的是方法签名不同,因此它们的类型也不同。

要解决此问题,您可以定义一个直接使用 *redis.Client 并满足新的 RedisClient 接口(interface)的瘦包装器。

type redisclient struct {
    rc *redis.Client
}

func (c *redisclient) Ping() RedisStatusCmd {
    return c.rc.Ping()
}

func NewRedisCliennt(options *redis.Options) RedisClient {
    // here wrap the *redis.Client into *redisclient
    return &redisclient{redis.NewClient(options)}
}

var newRedisClient = NewRedisClient

关于go - 从包函数返回 Mock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58016501/

相关文章:

go - Foo 类型的点 function() foo

time - 在 golang 中定义一种新的时间类型

go - 如何在 Go 中反转数组?

ssl - 如何在 Go ReverseProxy 中使用 TLS?

unit-testing - 测试在 Golang 中加载 JSON 配置文件的方法

go - 我应该通过“go build”编译到哪些平台?

unit-testing - 当多次调用一个函数时,是否有一种方法可以使AssertCalled每次调用

go - 如果关键测试失败如何中止测试

unit-testing - 使用 testify 使用不同的输入和输出模拟接口(interface)方法两次