返回模拟数据并退出闭包函数
来源:stackoverflow
时间:2024-03-18 13:12:30 284浏览 收藏
在编写测试时,模拟外部包函数的响应至关重要。然而,在 Go 中,模拟结构类型存在挑战,因为它们不可模拟。对于 redis.client 这样的结构类型,解决方案是定义一个接口,让结构类型隐式满足它。通过将模拟返回与新接口匹配,可以实现对结构类型方法的模拟。
我对 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
是一个结构类型,并且在go中结构类型根本不可可模拟。然而,go 中的接口是可模拟的,因此您可以定义自己的“newredisclient”函数,而不是返回结构体,而返回接口。由于 go 中的接口是隐式满足的,因此您可以定义您的接口,以便它将由 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
接口,因为 ping()
的返回类型不同。注意,redis.client.ping()
的结果类型是否满足 redisclient.ping()
返回的接口类型并不重要,重要的是方法签名不同,因此它们的类型不同。
要解决此问题,您可以定义一个直接使用 *redis.client
的瘦包装器,并且还满足新的 redisclient
接口。
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
本篇关于《返回模拟数据并退出闭包函数》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习