在Golang中制作模拟gin.Context

时间:2017-01-19 13:28:42

标签: unit-testing testing go mocking

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

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

以下是我的示例代码:

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

4 个答案:

答案 0 :(得分:7)

为了获得可以测试的*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,但使用上述方法可能更容易,因为它将执行和处理您的请求与实际请求相同。

答案 1 :(得分:5)

如果要将问题减少到"如何为函数参数创建模拟?"答案是:使用接口而不是具体类型。

JSON是一个具体类型的文字,Gin没有提供适当的界面。但你可以自己申报。由于您只使用Context中的type JSONer interface { JSON(code int, obj interface{}) } 方法,因此您可以声明超简单的界面:

JSONer

在所有期望Context作为参数的函数中使用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()
    }
}

现在很容易测试

{{1}}

Example simple as possible.

There is another question with a close sense

答案 2 :(得分:5)

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

答案 3 :(得分:0)

这里是一个示例,我如何模拟上下文,添加一个参数,在函数中使用它,如果存在非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))
}

相关问题