测试Go Echo中的HTTP处理程序

huangapple go评论74阅读模式
英文:

Test http handlers in go echo

问题

我正在学习Go的echo和单元测试,并且我陷入了困境,所以我来这里寻求帮助。

func TestGetGamesWithTags(t *testing.T){

    req := httptest.NewRequest("http.MethodGet", "/games?tags=tag0", nil)

    //response writer
    // we can inspect the ResponseRecorder output which is response generated by handler 
    recorder := httptest.NewRecorder()
    GlobalTestServer.echo.ServeHTTP(recorder, request)

    // 我不知道在这之后该怎么做

}

我不知道在这之后该怎么做。

GlobalTestServer.echo.ServeHTTP(recorder, request)
英文:

I am learning go echo and unit test and i am trapped into this and i am here to ask for help.

func TestGetGamesWithTags(t *testing.T){


	req := httptest.NewRequest("http.MethodGet", "/games?tags=tag0", nil)


	//response writer
	// we can inspect the ResponseRecorder output which is response generated by handler 
	recorder := httptest.NewRecorder()
	GlobalTestServer.echo.ServeHTTP(recorder, request)

	// i dont know what to do after this

}

I dont know what to do after

GlobalTestServer.echo.ServeHTTP(recorder, request)

答案1

得分: 1

为了测试你的echo处理程序,你需要一些东西,包括echo的contextrequestrecorder,下面是一个示例代码:

import (
    "net/http"
    "net/http/httptest"
    "net/url"

    "github.com/labstack/echo/v4"
    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/require"
)

func TestGetGamesWithTags(t *testing.T) {
    // 创建一个Echo实例
    e := echo.New()
    // 创建http测试记录器
    rec := httptest.NewRecorder()
    // 添加URL参数
    q := make(url.Values)
    q.Set("tags", "tag0")

    req := httptest.NewRequest(http.MethodGet, "/?"+q.Encode(), nil)
    // 创建新的echo上下文
    c := e.NewContext(req, rec)

    // 调用你的处理程序并传入echo上下文
    assert.NoError(t, getGamesWithTags(c))

    require.Equal(t, http.StatusOK, rec.Code)
    assert.JSONEq(
        t,
        `{"key":"value"}`,
        rec.Body.String(),
    )
}

这段代码用于测试一个名为getGamesWithTags的处理程序,它接受一个包含标签参数的GET请求,并返回JSON响应。你可以根据自己的需求修改代码和断言部分。

英文:

In order to test your echo handlers you need a bunch of things, the echo context, request, recorder, here is an example:

import (
  "net/http"
  "net/http/httptest"
  "net/url"

  "github.com/stretchr/testify/assert"
  "github.com/stretchr/testify/require"
)

func TestGetGamesWithTags(t *testing.T) {
    // Create an instance of Echo.
    e := echo.New() 
    // Create http test recorder
    rec := httptest.NewRecorder()
    // Add url params
    q := make(url.Values)
    q.Set("tags", "tag0")

    req := httptest.NewRequest(http.MethodGet, "/?"+q.Encode(), nil)
    // Create new echo context 
    c := e.NewContext(req, rec) 
    
    // Invoke your handlers against echo context
    assert.NoError(t, getGamesWithTags(c))
  
    require.Equal(t, http.StatusOK, rec.Code)
    assert.JSONEq(
	   t,
       `{"key":"value"}`,
       rec.Body.String(),
    )
}

答案2

得分: 0

如果你想对你的处理程序进行单元测试,一个好的做法是模拟你在处理程序中使用的服务,以隔离处理程序函数本身。

例如,你可以使用mockery自动生成模拟对象,并使用testify/mock来使用它们;你可以在互联网上轻松找到更多关于这些的信息,比如这篇文章介绍了一些内容:https://medium.com/@thegalang/testing-in-go-mocking-mvc-using-testify-and-mockery-c25344a88691。

英文:

If you want to unit test your handler, good practice would be to mock the services you are using in the handler to isolate the handler function itself.

For example, you can use mockery to generate automatically mocks, and testify/mock to use them ; You can easily find more information about these searching on internet, for instance this one for introduction : https://medium.com/@thegalang/testing-in-go-mocking-mvc-using-testify-and-mockery-c25344a88691.

huangapple
  • 本文由 发表于 2022年8月6日 14:12:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/73257572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定