28

I'm writing a REST API using Gin framework. But I was faced a trouble testing my controllers and researching TDD and Mock. I tried to apply TDD and Mock to my code but I could not.

I created a very reduced test environment and tried to create a controller test. How do I create a Mock for Gin.Context?

Here's my example code:

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]
}

My test example:

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)
}
*/
RimeBeliskner
  • 352
  • 3
  • 14
Wilson Tamarozzi
  • 606
  • 1
  • 8
  • 10
  • more comprehensive examples: [mocking requests and unit testing](https://stackoverflow.com/questions/66952761/how-to-unit-test-a-go-gin-handler-function), [mocking JSON binding](https://stackoverflow.com/questions/57733801/how-to-set-mock-gin-context-for-bindjson), [mocking gin.Context and implementing bindings or renderers](https://stackoverflow.com/questions/67508787/how-to-mock-a-gin-context), – blackgreen Sep 19 '21 at 09:30

4 Answers4

50

Gin provides the option to create a Test Context which you can use for whatever you need: https://godoc.org/github.com/gin-gonic/gin#CreateTestContext

Like that:

c, _ := gin.CreateTestContext(httptest.NewRecorder())
dmigo
  • 2,849
  • 4
  • 41
  • 62
Doppelganger
  • 20,114
  • 8
  • 31
  • 29
25

Here is an example of how I mock a context, add a param, use it in a function, then print the string of the response if there was a non-200 response.

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

mvndaai
  • 3,453
  • 3
  • 30
  • 34
16

In order to get a *gin.Context instance that you can test, you need a mock HTTP request and response. An easy way to create those is to use the net/http and net/http/httptest packages. Based on the code you linked, your test would look like this:

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

Although you could create a mock *gin.Context, it's probably easier to use the method above, since it'll execute and handle your request the same as it would an actual request.

bentranter
  • 401
  • 3
  • 8
12

If to reduce the question to "How to create mock for a function argument?" the answer is: use interfaces not concrete types.

type Context struct is a concrete type literal and Gin doesn't provide appropriate interface. But you can declare it by yourself. Since you are using only JSON method from Context you can declare extra-simple interface:

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

And use JSONer type instead Context type in all your functions which expect Context as argument:

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

And now it is easy to test

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

Community
  • 1
  • 1
I159
  • 29,741
  • 31
  • 97
  • 132
  • 1
    Yes, now I could run tests, but now I could not build project, because I get error "cannot use pingController.Ping (type func(controllers.JSONContextInterface)) as type gin.HandlerFunc in argument to router.RouterGroup.GET" – Bunyk Sep 07 '17 at 08:58
  • @Bunyk, it happens when your mock struct doesn't satisfy the interface. Check methods receivers. – I159 Sep 07 '17 at 09:08
  • @I159 I run into the same problem. `router.RouterGroup.GET` expects a HandlerFunc as its 2nd argument. HandlerFunc is an alias for `func(*gin.Context)`. You might define JSONer so that *gin.Context implements JSONer, but I don't think JSONer could ever have type *gin.Context. – Ian Jul 06 '22 at 21:46