Unit testing 制作模拟杜松子酒。歌朗的背景

Unit testing 制作模拟杜松子酒。歌朗的背景,unit-testing,go,testing,mocking,go-gin,Unit Testing,Go,Testing,Mocking,Go Gin,我正在使用Gin框架编写RESTAPI。但是我在测试我的控制器和研究TDD和Mock时遇到了麻烦。我试图将TDD和Mock应用到我的代码中,但我做不到 我创建了一个非常简化的测试环境,并尝试创建一个控制器测试。如何为Gin.Context创建一个Mock 下面是我的示例代码: package main import ( "strconv" "github.com/gin-gonic/gin" ) // MODELS type Users []User type User st

我正在使用Gin框架编写RESTAPI。但是我在测试我的控制器和研究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)
}
*/
我的测试示例:

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.Context
实例,您需要一个模拟HTTP请求和响应。创建它们的一个简单方法是使用
net/http
net/http/httptest
包。根据链接的代码,您的测试如下所示:

主程序包
进口(
“net/http”
“net/http/httptest”
“测试”
“github.com/gin gonic/gin”
)
func TestControllerGetAll(t*testing.t){
//切换到测试模式,这样您就不会得到如此嘈杂的输出
gin.SetMode(gin.TestMode)
//设置路由器,就像在主功能中一样,然后
//登记你的路线
r:=gin.Default()
r、 获取(“/users”,GetUsers)
//创建要测试的模拟请求。确保第二个参数
//这与您在路由器设置中定义的一条路由相同
//拦住!
req,err:=http.NewRequest(http.MethodGet,“/users”,nil)
如果错误!=零{
t、 Fatalf(“无法创建请求:%v\n”,错误)
}
//创建响应记录器,以便可以检查响应
w:=httptest.NewRecorder()
//执行请求
r、 ServeHTTP(带,需要)
//检查响应是否符合您的预期
如果w.Code!=http.StatusOK{
t、 Fatalf(“预期状态为%d,但实际状态为%d\n”,http.StatusOK,w.Code)
}
}

尽管您可以创建一个mock
*gin.Context
,但是使用上面的方法可能更容易,因为它会像处理实际请求一样执行和处理您的请求。

如果要将问题归结为“如何为函数参数创建mock?”答案是:使用接口而不是具体类型

type-Context-struct
是一个具体的类型文本,Gin没有提供适当的接口。但是你可以自己申报。由于您仅使用
Context
中的
JSON
方法,因此可以声明额外的简单接口:

type JSONer interface {
    JSON(code int, obj interface{})
}
并在所有希望使用
Context
作为参数的函数中使用
JSONer
type代替
Context
type:

/* 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()
    }
}

Gin提供了创建测试上下文的选项,您可以根据需要使用该上下文:

就像这样:


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

下面是一个示例,演示如何模拟上下文,添加参数,在函数中使用它,然后打印响应字符串(如果有非200响应)

gin.SetMode(gin.TestMode)
w:=httptest.NewRecorder()
c、 _u:=gin.CreateTestContext(w)
c、 Params=[]gin.Param{gin.Param{Key:“k”,Value:“v”}
傅(c)
如果w.代码!=200 {
b、 _Util:=ioutil.ReadAll(w.Body)
t、 错误(w代码,字符串(b))
}

请添加链接到问题的代码,以防被删除,这样人们就可以更容易地理解这个问题。哦,天哪,这太简单了!这正是我想要的。在本例中,我将制作一个适配器,以防我想要更改框架。多谢各位@Pandemonio,我很高兴它有帮助:)是的,现在我可以运行测试,但现在我无法构建项目,因为我得到错误“无法使用pingController.Ping(type func(controllers.JSONContextInterface))作为router.RouterGroup.get参数中的gin.HandlerFunc类型“@Bunyk,当您的模拟结构不满足接口时会发生这种情况。检查方法接收者。在这里有一个完整的示例会很有用。
c,:=gin.CreateTestContext(httptest.NewRecorder())
这对我很有效,因为我想隔离我的处理程序方法。谢谢这算是单元测试还是集成测试?这是集成测试而不是单元测试。单元测试不需要启动http服务器