unit-testing - 在 Golang 中制作模拟 gin.Context

标签 unit-testing go testing mocking go-gin

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

我创建了一个非常简化的测试环境,并尝试创建一个 Controller 测试。如何为 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)
}
*/

最佳答案

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

像那样:

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

关于unit-testing - 在 Golang 中制作模拟 gin.Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41742988/

相关文章:

java - Powermockito 私有(private)方法模拟 NullPointerException。调用私有(private)方法

unit-testing - Angular:如何调试单元测试中的注入(inject)错误

sql - postgres - 以编程方式将 1gb 模拟数据加载到表中的方法

json - 是否可以在JSON中创建自定义对象键? [复制]

angularjs - 测试返回 promise 的 Angular 服务

phpunit 只处理一个文件而不是多个

java - 有什么好的框架可以自动测试 AWT GUI 吗?

go - 迭代模板 slice 中可变数量的项目

go - 在 golang 中捕获 stdErr 输出

Python测试整个脚本