我想为控制器编写单元测试,但我不断收到运行时错误。我发现这是由于没有Host关于请求、ClientIP()方法和请求正文的。如何在测试上下文中设置它们?
这是我到目前为止得到的。它与Host: c.Request.Host.
控制器:
type loggingControllers struct {
LoggingService services.InterfaceLogging
}
func (l *loggingControllers) RegisterError(c *gin.Context) {
errorEvent := models.Error{
Badges: map[string]string{},
Host: c.Request.Host,
ClientIP: c.ClientIP(),
UserAgent: c.Request.UserAgent(),
Count: 1,
Timestamp: time.Now().Unix(),
}
err := json.NewDecoder(c.Request.Body).Decode(&errorEvent)
if err != nil {
utils.RespondWithError(c, http.StatusInternalServerError, err.Error())
return
}
go l.LoggingService.SaveError(errorEvent)
utils.RespondWithSuccess(c)
}
func GetLoggingControllerMock() loggingControllers {
loggingServiceMock := services.GetLoggingServiceMock()
return loggingControllers{
LoggingService: &loggingServiceMock,
}
}
单元测试:
func TestLoggingControllers(t *testing.T) {
loggingControllers := GetLoggingControllerMock()
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
loggingControllers.RegisterError(c)
}
错误信息:
--- FAIL: TestLoggingControllers (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=0x38 pc=0x15238ec]
catspeake
相关分类