Home > Backend Development > Golang > golang unit test failure expected zero, but got: 'null\n'

golang unit test failure expected zero, but got: 'null\n'

WBOY
Release: 2024-02-06 09:03:08
forward
673 people have browsed it

golang 单元测试失败预期为零,但得到:“null\n”

Question content

First of all, I am trying to learn golang. I'm trying to unit test my handlers in golang using testify package. When running my test I get this error -> Expected nil, but got: "null\n" . I'm surprised it shows the method returning a null value. As far as I know, golangte has nil instead of null. what is the reason?

my handler test file

package entities

import (
    "github.com/google/uuid"
    "github.com/labstack/echo/v4"
    "github.com/stretchr/testify/assert"
    "net/http"
    "net/http/httptest"
    "strings"
    "testing"
)

type mock struct {
    create  func(user User) error
    getById func(id uuid.UUID) *User
}

var userJson = `{"id":"e918b0bc-76f3-4380-a1fb-a14ca5a08fc3","name":"Jon Snow","email":"[email protected]"}`

func (m *mock) Create(user User) error     { return m.create(user) }
func (m *mock) GetById(id uuid.UUID) *User { return m.getById(id) }

func TestCreate(t *testing.T) {
    t.Parallel()
    //Arrange
    e := echo.New()
    expectedStatusCode := http.StatusCreated
    m := &mock{create: func(user User) error { return nil }}
    req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJson))
    req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
    rec := httptest.NewRecorder()
    c := e.NewContext(req, rec)
    server := NewUserHandler(m)

    //Act
    server.Create(c)

    //Assert
    assert.Equal(t, expectedStatusCode, rec.Code)
    assert.Nil(t, rec.Body.String())
}


my handler

func (u *userHandler) Create(e echo.Context) error {

    var user User
    err := json.NewDecoder(e.Request().Body).Decode(&user)
    if err != nil {
        e.JSON(http.StatusUnprocessableEntity, err)
    }
    u.userService.Create(user)
    if err != nil {
        e.JSON(http.StatusBadRequest, err)
    }
    return e.JSON(http.StatusCreated, nil)
}

 my user service

 func (u *userService) Create(user User) (err error) {
    err = u.userRepo.Create(user)
    return
}
Copy after login


Correct answer


You are sending back JSON. When you marshal nil, it becomes "null".

In your tests you would do this

assert.Nil(t, rec.Body.String())
Copy after login

and rec.Body is JSON encoded nil (i.e. "null")

In fact, because rec.Body.String() returns string instead of *string, this test will never pass.

The above is the detailed content of golang unit test failure expected zero, but got: 'null\n'. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template