英文:
go - How to mock fiber context
问题
我一直在尝试模拟一个fiber.Ctx,但是我无法使其工作,我一直得到这个错误:
--- FAIL: TestCheckHeaders (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x12085f0]
我正在尝试测试的代码:
CheckHeaders.go
package middleware
import "github.com/gofiber/fiber/v2"
func CheckHeaders(c *fiber.Ctx) error {
headers := c.GetReqHeaders()
if headers["headerValue"] == "true"{
return c.Next()
} else {
return c.SendStatus(401)
}
}
CheckHeaders_test.go
package middleware
import (
"testing"
"github.com/gofiber/fiber/v2"
)
func TestCheckHeaders(t *testing.T) {
type args struct {
c *fiber.Ctx
}
fiberContext := fiber.Ctx{}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "test 1",
args: args{c: &fiberContext},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CheckHeaders(tt.args.c); (err != nil) != tt.wantErr {
t.Errorf("CheckHeaders() error = %v, wantErr %v", err,tt.wantErr)
}
})
}
}
英文:
I've been trying to mock a fiber.Ctx but I have not been able to make it work I have been getting this error:
>--- FAIL: TestCheckHeaders (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x12085f0]
The code that I am trying to test:
CheckHeaders.go
package middleware
import "github.com/gofiber/fiber/v2"
func CheckHeaders(c *fiber.Ctx) error {
headers := c.GetReqHeaders()
if headers["headerValue"] == "true"{
return c.Next()
} else {
return c.SendStatus(401)
}
}
CheckHeaders_test.go
package middleware
import (
"testing"
"github.com/gofiber/fiber/v2"
)
func TestCheckHeaders(t *testing.T) {
type args struct {
c *fiber.Ctx
}
fiberContext := fiber.Ctx{}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "test 1",
args: args{c: &fiberContext},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CheckHeaders(tt.args.c); (err != nil) != tt.wantErr {
t.Errorf("CheckHeaders() error = %v, wantErr %v", err,tt.wantErr)
}
})
}
}
答案1
得分: 2
这是我设置一个新的上下文以供测试操作的方式。希望对你有所帮助。
app := fiber.New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
c
将是一个全新的 *fiber.Ctx
。
英文:
This was how I set up a new context that I could manipulate for testing. Hope it helps.
app := fiber.New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
c
will be a fresh *fiber.Ctx
.
答案2
得分: 0
你可以使用fiber.Ctx
结构体中需要使用的方法创建自己的接口,并为该接口创建模拟。
然后,你可以将fiber.Ctx
处理为例如Ctxer
接口的实现,这样你就可以使用https://github.com/golang/mock来模拟它并在测试中使用模拟。
代码示例如下:
type Ctxer interface {
GetReqHeaders() map[string]string
Next() (err error)
SendStatus(status int) error
}
func CheckHeaders(c Ctxer) error {
headers := c.GetReqHeaders()
if headers["headerValue"] == "true" {
return c.Next()
} else {
return c.SendStatus(401)
}
}
以上是要翻译的内容。
英文:
You can create your own interface with methods you need to use from fiber.Ctx
struct and make mock for the interface.
After that you are going to handle fiber.Ctx
as implementation of for example Ctxer
interface so you will be able to mock it using https://github.com/golang/mock and use the mock in your tests.
It's gonna look like this
type Ctxer interface {
GetReqHeaders() map[string]string
Next() (err error)
SendStatus(status int) error
}
func CheckHeaders(c Ctxer) error {
headers := c.GetReqHeaders()
if headers["headerValue"] == "true" {
return c.Next()
} else {
return c.SendStatus(401)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论