我需要为其中一个处理程序编写一个测试.在处理程序中,我有一些类似的东西:
ctx.Params("id")
个
有没有可能创建一个上下文,使处理程序内部的参数不为零?
我try 使用ctx.route().Params更改参数字段,但不起作用
我需要为其中一个处理程序编写一个测试.在处理程序中,我有一些类似的东西:
ctx.Params("id")
个
有没有可能创建一个上下文,使处理程序内部的参数不为零?
我try 使用ctx.route().Params更改参数字段,但不起作用
我认为最好使用(*App).Test,并让它从请求中创建一个上下文.就像这样:
package main
import (
"fmt"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
)
func handler(c *fiber.Ctx) error {
id := c.Params("id")
fmt.Println("Params:", id)
return nil
}
func TestXxx(t *testing.T) {
app := fiber.New()
app.Get("/hello/:id", handler)
req := httptest.NewRequest("GET", "/hello/man", nil)
_, _ = app.Test(req, -1)
}
$ go test . -v
=== RUN TestXxx
Params: man
--- PASS: TestXxx (0.00s)
PASS
ok m 0.002s