【问题标题】:Make mock gin.Context in Golang在 Golang 中制作模拟 gin.Context
【发布时间】:2017-06-04 05:20:47
【问题描述】:

我正在使用 Gin 框架编写一个 REST API。但是我在测试控制器和研究 TDD 和 Mock 时遇到了麻烦。我尝试将 TDD 和 Mock 应用于我的代码,但我做不到。

我创建了一个非常精简的测试环境并尝试创建一个控制器测试。如何为 Gin.Context 创建 Mock?

这是我的示例代码:

package main

import (
    "strconv"
    "github.com/gin-gonic/gin"
)

// MODELS
type Users []User
type User struct {
    Name string `json"name"`
}


func main() {
    r := gin.Default()

    r.GET("/users", GetUsers)
    r.GET("/users/:id", GetUser)

    r.Run(":8080")
}

// ROUTES
func GetUsers(c *gin.Context) {
    repo := UserRepository{}
    ctrl := UserController{}

    ctrl.GetAll(c, repo)
}

func GetUser(c *gin.Context) {
    repo := UserRepository{}
    ctrl := UserController{}

    ctrl.Get(c, repo)
}

// CONTROLLER
type UserController struct{}

func (ctrl UserController) GetAll(c *gin.Context, repository UserRepositoryIterface) {
    c.JSON(200, repository.GetAll())
}

func (ctrl UserController) Get(c *gin.Context, repository UserRepositoryIterface) {

    id := c.Param("id")

    idConv, _ := strconv.Atoi(id)

    c.JSON(200, repository.Get(idConv))
}

// REPOSITORY
type UserRepository struct{}
type UserRepositoryIterface interface {
    GetAll() Users
    Get(id int) User
}

func (r UserRepository) GetAll() Users {
    users := Users{
        {Name : "Wilson"},
        {Name : "Panda"},
    }

    return users
}

func (r UserRepository) Get(id int) User {
    users := Users{
        {Name : "Wilson"},
        {Name : "Panda"},
    }

    return users[id-1]
}

我的测试示例:

package main

import(
    "testing"
    _ "github.com/gin-gonic/gin"
)

type UserRepositoryMock struct{}

func (r UserRepositoryMock) GetAll() Users {
    users := Users{
        {Name : "Wilson"},
        {Name : "Panda"},
    }

    return users
}

func (r UserRepositoryMock) Get(id int) User {
    users := Users{
        {Name : "Wilson"},
        {Name : "Panda"},
    }

    return users[id-1]
}


// TESTING REPOSITORY FUNCTIONS
func TestRepoGetAll(t *testing.T) {

    userRepo := UserRepository{}

    amountUsers := len(userRepo.GetAll())

    if amountUsers != 2 {
        t.Errorf("Esperado %d, recebido %d", 2, amountUsers)
    }
}

func TestRepoGet(t *testing.T) {

    expectedUser := struct{
        Name string
    }{
        "Wilson",
    }

    userRepo := UserRepository{}

    user := userRepo.Get(1)

    if user.Name != expectedUser.Name {
        t.Errorf("Esperado %s, recebido %s", expectedUser.Name, user.Name)
    }
}

/* HOW TO TEST CONTROLLER?
func TestControllerGetAll(t *testing.T) {
    gin.SetMode(gin.TestMode)
    c := &gin.Context{}
    c.Status(200)
    repo := UserRepositoryMock{}
    ctrl := UserController{}

    ctrl.GetAll(c, repo)
}
*/

【问题讨论】:

标签: unit-testing go testing mocking go-gin


【解决方案1】:

Gin 提供了创建测试上下文的选项,您可以根据需要使用它: https://godoc.org/github.com/gin-gonic/gin#CreateTestContext

这样:

c, _ := gin.CreateTestContext(httptest.NewRecorder())

【讨论】:

    【解决方案2】:

    这是我如何模拟上下文、添加参数、在函数中使用它,然后在有非 200 响应时打印响应字符串的示例。

    gin.SetMode(gin.TestMode)
    
    w := httptest.NewRecorder()
    c, _ := gin.CreateTestContext(w)
    
    c.Params = []gin.Param{gin.Param{Key: "k", Value: "v"}}
    
    foo(c)
    
    if w.Code != 200 {
        b, _ := ioutil.ReadAll(w.Body)
        t.Error(w.Code, string(b))
    }
    
    

    【讨论】:

      【解决方案3】:

      为了获得可以测试的*gin.Context 实例,您需要一个模拟HTTP 请求和响应。创建它们的一种简单方法是使用net/httpnet/http/httptest 包。根据您链接的代码,您的测试将如下所示:

      package main
      
      import (
          "net/http"
          "net/http/httptest"
          "testing"
      
          "github.com/gin-gonic/gin"
      )
      
      func TestControllerGetAll(t *testing.T) {
      
          // Switch to test mode so you don't get such noisy output
          gin.SetMode(gin.TestMode)
      
          // Setup your router, just like you did in your main function, and
          // register your routes
          r := gin.Default()
          r.GET("/users", GetUsers)
      
          // Create the mock request you'd like to test. Make sure the second argument
          // here is the same as one of the routes you defined in the router setup
          // block!
          req, err := http.NewRequest(http.MethodGet, "/users", nil)
          if err != nil {
              t.Fatalf("Couldn't create request: %v\n", err)
          }
      
          // Create a response recorder so you can inspect the response
          w := httptest.NewRecorder()
      
          // Perform the request
          r.ServeHTTP(w, req)
      
          // Check to see if the response was what you expected
          if w.Code != http.StatusOK {
              t.Fatalf("Expected to get status %d but instead got %d\n", http.StatusOK, w.Code)
          }
      }
      

      虽然您可以创建一个模拟 *gin.Context,但使用上述方法可能更容易,因为它会像处理实际请求一样执行和处理您的请求。

      【讨论】:

      • 这算作单元测试还是集成测试?
      • 这是一个集成测试而不是单元。单元测试不需要启动 http 服务器
      【解决方案4】:

      如果将问题简化为“如何为函数参数创建模拟?”答案是:使用接口而不是具体类型。

      type Context struct 是一个具体的类型文字,Gin 没有提供适当的接口。但是你可以自己声明。由于您只使用来自ContextJSON 方法,您可以声明超简单接口:

      type JSONer interface {
          JSON(code int, obj interface{})
      }
      

      在所有期望Context作为参数的函数中使用JSONer类型而不是Context类型:

      /* Note, you can't declare argument as a pointer to interface type,
         but when you call it you can pass pointer to type which
         implements the interface.*/
      func GetUsers(c JSONer) {
          repo := UserRepository{}
          ctrl := UserController{}
      
          ctrl.GetAll(c, repo)
      }
      
      func GetUser(c JSONer) {
          repo := UserRepository{}
          ctrl := UserController{}
      
          ctrl.Get(c, repo)
      }
      
      func (ctrl UserController) GetAll(c JSONer, repository UserRepositoryIterface) {
          c.JSON(200, repository.GetAll())
      }
      
      func (ctrl UserController) Get(c JSONer, repository UserRepositoryIterface) {
      
          id := c.Param("id")
      
          idConv, _ := strconv.Atoi(id)
      
          c.JSON(200, repository.Get(idConv))
      }
      

      现在很容易测试

      type ContextMock struct {
          JSONCalled bool
      }
      
      func (c *ContextMock) JSON(code int, obj interface{}){
          c.JSONCalled = true
      }
      
      func TestControllerGetAll(t *testing.T) {
          gin.SetMode(gin.TestMode)
          c := &ContextMock{false}
          c.Status(200)
          repo := UserRepositoryMock{}
          ctrl := UserController{}
      
          ctrl.GetAll(c, repo)
      
          if c.JSONCalled == false {
              t.Fail()
          }
      }
      

      Example simple as possible.

      There is another question with a close sense

      【讨论】:

      • 是的,现在我可以运行测试,但现在我无法构建项目,因为我收到错误“无法使用 pingController.Ping (type func(controllers.JSONContextInterface)) as type gin.HandlerFunc in argument到 router.RouterGroup.GET"
      • @Bunyk,当您的模拟结构不满足接口时会发生这种情况。检查方法接收者。
      猜你喜欢
      • 2021-08-03
      • 1970-01-01
      • 2020-01-04
      • 2021-07-24
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 2017-09-29
      • 2019-01-18
      相关资源
      最近更新 更多