测试Golang中的handler中调用该方法
来源:stackoverflow
2024-04-21 17:09:35
0浏览
收藏
学习Golang要努力,但是不要急!今天的这篇文章《测试Golang中的handler中调用该方法》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!
问题内容
我正在 golang 中实现一个 api。我有一个端点,我在其中调用带有其他包的参数的方法。现在我需要检查请求中是否已调用该方法。
下面是我正在做的和我期待的类似的小场景。
我的处理程序
package mypackage
import (
"log"
"github.com/myrepo/notifier" // my another package
)
func myhandler(writer http.responsewriter, request *http.request) {
// ...
// ...
notifier.notify(4, "sdfsdf")
// ...
// ...
}
测试处理程序
func TestMyHandler(t *testing.T) {
// Here I want to
req, _ := http.NewRequest("GET", "/myendpoint", nil)
// ... Want to test that notifier.Notify is called
// ...
}
在 testmyhandler 中,我想检查 notifier.notify 是否已调用。
调查结果
我试图理解 assertnumberofcalls、func (*mock) called 和 func (*mock) methodcalled,但我不确定如何使用它们:(。
我是 golang 的新手,对此我感到非常兴奋。 如果我遗漏了任何内容,请告诉我,否则您可能需要更多信息才能更好地理解。
解决方案
这是使用依赖注入和接口的好机会。
也就是说,我们需要提取一个notifier的概念
(警告:代码未直接测试)
type notifier interface {
notify(int, string)() error
}
现在,为了避免与 notifier 库产生任何混淆,请使用本地别名。
import "github.com/myrepo/notifier" mynotifier
然后,因为您使用的库将其导出为函数,而不是在结构中,所以我们需要创建一个实现我们的接口的结构
type mynotifier struct {}
func (mn *mynotifier) notify(n int, message string) error {
return mynotifier.notify(n, message)
}
然后修改你的函数:
func myhandler(writer http.responsewriter, request *http.request, notifier notifier) {
// ...
// ...
notifier.notify(4, "sdfsdf")
// ...
// ...
}
然后在您的测试中,您现在可以自由发送间谍通知程序
type spynotifier struct {
called boolean
}
func (n *spynotifier) notify(n int, msg string) error {
n.called = true
return
}此方法与 Mathew's answer 类似,但使用 testify 中的 mock 包。在这里,您创建通知程序的模拟实现,注册方法调用,并断言已使用预期参数调用该方法。
实施
package handler
import (
"net/http"
"github.com/stretchr/testify/mock"
)
// the interface for the notifier
type notifier interface {
notify(int, string) error
}
// the mock implementation of the interface above
type mocknotifier struct {
mock.mock
}
// stub the notify method to ensure it can be expected later in the test
func (mocknotifier *mocknotifier) notify(arg1 int, arg2 string) error {
args := mocknotifier.called(arg1, arg2)
return args.error(0)
}
// this handler which accepts a notifier for dependency injection
type handler struct {
notifier notifier
}
// the myhandler implementation which calls the notifier instance
func (h *handler) myhandler(writer http.responsewriter, request *http.request) {
// this is what we want to test!
h.notifier.notify(4, "sdfsdf")
}
测试
package handler_test
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestMyHandler(t *testing.T) {
t.Parallel()
mockNotifier := MockNotifier{}
handler := Handler{ notifier: &mockNotifier }
// register the mock to expect a call to Notify with the given arguments and return nil as the error
mockNotifier.On("Notify", 4, "sdfsdf").Return(nil)
// setup the test arguments
request := httptest.NewRequest(http.MethodGet, "/someapi", nil)
writer := httptest.NewRecorder()
// call the handler
handler.MyHandler(writer, request)
// this is the important part!!
// this ensures that the mock Notify method was called with the correct arguments, otherwise the test will fail
mockNotifier.AssertExpectations(t)
}终于介绍完啦!小伙伴们,这篇关于《测试Golang中的handler中调用该方法》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
从其他项目 protobuf 文件导入消息类型
- 上一篇
- 从其他项目 protobuf 文件导入消息类型
- 下一篇
- 使用 Google Cloud go 库进行日志记录时出现问题
查看更多
最新文章
-
- Golang · Go问答 | 6小时前 | go · RGBA image ColorModel At
- Go image 怎样读取不同颜色模型的像素
- 291浏览 收藏
-
- Golang · Go问答 | 6小时前 |
- Go 怎样把 pprof 挂到独立 ServeMux
- 491浏览 收藏
-
- Golang · Go问答 | 6小时前 |
- Go embed FS 打开不存在资源时怎样保留错误上下文
- 141浏览 收藏
-
- Golang · Go问答 | 6小时前 |
- Go template.Funcs 怎样在 ParseFS 前注册自定义函数
- 406浏览 收藏
-
- Golang · Go问答 | 6小时前 |
- Go XML Decoder Strict 关闭后适合什么兼容场景
- 412浏览 收藏
-
- Golang · Go问答 | 6小时前 |
- Go 怎样读取子进程资源使用量
- 373浏览 收藏
-
- Golang · Go问答 | 6小时前 |
- Go os/user Current 和 Lookup 怎样选择
- 171浏览 收藏
-
- Golang · Go问答 | 6小时前 | 权限 · Go问答 · Go Unix Socket UnixConn 对端凭据 SO_PEERCRED x/sys/unix
- Go Unix socket 怎样读取对端凭据
- 302浏览 收藏
-
- Golang · Go问答 | 6小时前 |
- Go Unix 数据报套接字怎样处理报文边界
- 336浏览 收藏
-
- Golang · Go问答 | 6小时前 |
- Go 项目怎样为 HTTP/3 客户端划分可选依赖
- 471浏览 收藏
-
- Golang · Go问答 | 6小时前 |
- Go HTTP/2 异常怎样用 GODEBUG 获取线索
- 366浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 477次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 534次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 554次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 506次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 327次使用
查看更多
相关文章
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go crypto/rand.Text 的长度为什么不是固定字符数
- 2026-10-04 501浏览
-
- Go strings.ToValidUTF8 清洗日志内容的边界
- 2026-10-03 501浏览
-
- Go tls.GetCertificate 为什么收不到空 ServerName 请求
- 2026-09-27 501浏览
