- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我一直在尝试模拟 gin.Context 但我无法让它工作
我正在尝试他们在 solution 中所做的事情但它不适用于我的路由器这是我遇到的错误r.POST("/urls", urlRepo.CreateUrl)
cannot use urlRepo.CreateUrl (value of type func(c controllers.Icontext)) as gin.HandlerFunc value in argument to r.POSTcompilerIncompatibleAssign
这是我为稍后模拟而创建的接口(interface)以及我将在其中进行测试的方法
type Icontext interface {
BindJSON(obj interface{}) error
JSON(code int, obj interface{})
AbortWithStatus(code int)
AbortWithStatusJSON(code int, jsonObj interface{})
}
func (repository *UrlRepo) CreateUrl(c Icontext) {
var url models.Url
c.BindJSON(&url)
if !validators.IsCreateJsonCorrect(url) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Error format in Short or Full"})
return
}
err := repository.reposito.CreateUrl(repository.Db, &url)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err})
return
}
c.JSON(http.StatusOK, url)
}
代替
func (repository *UrlRepo) CreateUrl(c Icontext)
它是
func (repository *UrlRepo) CreateUrl(c *gin.Context)
最佳答案
严格来说,你不能“ mock ”一个 *gin.Context
以一种有意义的方式,因为它是 struct
带有未导出的字段和方法。
此外,您不能传递给 r.POST()
类型不是 gin.HandlerFunc
的函数, 定义为 func(*gin.Context)
.您的处理程序的类型 CreateUrl(c Icontext)
根本不匹配。
如果您的目标是对 Gin 处理程序进行单元测试,那么您绝对不必模拟 *gin.Context
.您应该做的是在其中设置测试值。为此,您可以简单地使用 gin.CreateTestContext()
并手动初始化其中一些字段。更多信息 here .
如果出于其他原因,您的目标是提供 *gin.Context
功能的替代实现。为了在您的处理程序中使用,您可以做的是使用您自己的替代方法定义您自己的类型并嵌入 *gin.Context
在里面。
在实践中:
type MyGinContext struct {
*gin.Context
}
func (m *MyGinContext) BindJSON(obj interface{}) error {
fmt.Println("my own BindJSON")
return m.Context.BindJSON(obj) // or entirely alternative implementation
}
// Using the appropriate function signature now
func (repository *UrlRepo) CreateUrl(c *gin.Context) {
myCtx := &MyGinContext{c}
var url models.Url
_ = myCtx.BindJSON(&url) // will also print "my own BindJSON"
// ...
// other gin.Context methods are promoted and available on MyGinContext
myCtx.Status(200)
}
但老实说,我不确定您为什么要覆盖
*gin.Context
的某些方法。 .如果你想提供不同的绑定(bind)逻辑,甚至不同的渲染,你可以实现库已经暴露的接口(interface)。例如:
c.ShouldBindWith()
将接口(interface)
binding.Binding
作为第二个参数您可以实现:
type MyBinder struct {
}
func (m *MyBinder) Name() string {
return "foo"
}
func (m *MyBinder) Bind(*http.Request, interface{}) error {
// stuff
return nil
}
func MyHandler(c *gin.Context) {
var foo struct{/*fields*/}
c.ShouldBindWith(&foo, &MyBinder{})
}
实现一个渲染器:
type MyRenderer struct {
}
type Render interface {
func (m *MyRenderer) Render(http.ResponseWriter) error {
// ...
return nil
}
func (m *MyRenderer) WriteContentType(w http.ResponseWriter) {
header := w.Header()
if val := header["Content-Type"]; len(val) == 0 {
header["Content-Type"] = "application/foo+bar"
}
}
func MyHandler(c *gin.Context) {
c.Render(200, &MyRenderer{})
}
关于go - 如何模拟 gin.Context?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67508787/
我有很多路线,从 gin.Default() 开始使用 Gin (默认情况下为所有路由启用日志记录和恢复)。但是有一条路由(即 /health )每 5 秒被 ping 一次。在不更改大部分代码的情况
gin 是目前 Go 里面使用最广泛的框架之一了,弄清楚 gin 框架的原理,有助于我们更好的使用 gin。这个系列 gin 源码阅读会逐步讲明白 gin 的原理,欢迎关注后续文章。 gin
我从 gin 文档中了解到,您可以将 json 绑定(bind)到类似 的结构 type Login struct { User string `form:"user" json:"u
我正在使用 API,GET 和 POST 工作正常,除非我尝试通过其 ID 获取选择记录(例如/articles/2)。文章存在,当通过/articles 路径检索所有记录时,我得到了正确的响应。这是
我找不到在 UNIX 中正确执行此代码的方法: package main import ( "time" "github.com/gin-gonic/gin" "net/http" ) t
我尝试使用作为 Golang 框架的 Gin。 https://github.com/gin-gonic/gin 我从官方github上复制了示例代码。 就像这样。 package main impo
下面访问gin-gonic服务器时,HTTP客户端应该收到code 500,却收到code 200。 package main import ( "github.com/gin-contrib
我使用 Guice/Gin 设计了一个新项目,因此我可以使我们的代码更加模块化和可交换,尤其是在测试时。 但是,我无法弄清楚如何在实践中完成这项工作。我的印象是我可以在我的测试中创建一个新的 Gin/
我正在尝试在 golang:alpine docker 镜像上运行一个非常简单的 go/gin 程序。这无法运行/构建。 运行容器: docker run -it --rm golang:1.10.1
本文介绍如何通过 rk-boot 快速搭建静态文件下载 Web 服务。什么是 静态文件下载 Web UI?通过配置文件,快速搭建可下载文件的 Web 服务。 请访问如下地址获取完整教程: r
这是我的一个例子 - public class GinDemoPresenter implements Presenter { private View view; @Inject
我正在尝试使用一种干净的方法,使用传递给我的类的父上下文来优雅地停止我的gin服务器。 这是我当前的代码 有没有更清洁的方法可以做到这一点?感觉像很多样板代码和不必要的例如使用无限循环完成如此简单的任
我有以下代码 public class AppGinModule extends AbstractGinModule{ @Override protected void configu
我正在使用OPTIONS方法获得204,但是似乎没有碰到终点 只需构建一个简单的文件上传端点,如下所示: package main import ( "cloud.google.com/go
在对其数据执行验证后,我试图用它的数据恢复上下文。我需要数据在下一个函数中根据需要继续移动。 我是 golang 的新手,下面的代码是我所能做的。非常感谢任何帮助和更好的方法。 提前致谢。 验证中间件
有一个使用gin-gonic框架以golang编写的服务。 我只想将application/json支持为mime类型,并且如果它始终以UTF-8格式运行,那就太好了。如果服务的业务逻辑将获得不同编码
我试图在我的 Handler 上进行一些处理后传递一些变量功能。我该怎么办 redirect到一个新页面并将一些 json 变量传递给这个新模板? // main package func main(
我运行关于文件上传的 gin 示例,这个 repo 来自 https://github.com/gin-gonic/examples/tree/5898505356e9064c49abb075eae8
我收到以下错误消息: controllers/user.go:4:2: cannot find package "(underscore)/home/ubuntu/goapi/src/github.c
我需要在中间件函数中操作响应数据。假设我有产品处理程序和客户处理程序。产品处理程序返回产品列表,客户返回客户列表。在中间件函数中,我想将这些响应转换为 ApiResponse 结构。 type Api
我是一名优秀的程序员,十分优秀!