为了账号安全,请及时绑定邮箱和手机立即绑定

panic: open template/layout-main-page.html:

panic: open template/layout-main-page.html:

Go
德玛西亚99 2023-02-21 19:12:07
我是 Golang 的新手,我试图在我的应用程序中运行一个简单的测试,但每次都会发生恐慌。我找不到问题所在。我看过几个地方,但找不到解决方案。随时问我关于代码的任何问题,如果你有任何问题,尽管问。错误信息:PS D:\projetos go\api-ranking-crypto> go test ./test/panic: open template/layout-main-page.html: The system cannot find the path specified.goroutine 1 [running]:html/template.Must(...)        D:/Go/src/html/template/template.go:374github.com/maickmachado/upvote-api/controllers.init()        D:/projetos go/api-ranking-crypto/controllers/controllers.go:15 +0x1d8FAIL    github.com/maickmachado/upvote-api/test 0.209sFAIL我的测试代码:    func TestHealthCheck(t *testing.T) {    tt := []struct {        name   string        method string        statusCode int    }{        {            name:   "status ok",            method: http.MethodGet,            statusCode: http.StatusOK,        },    }    for _, tc := range tt {        t.Run(tc.name, func(t *testing.T) {            request := httptest.NewRequest(tc.method, "/healthcheck", nil)            responseRecorder := httptest.NewRecorder()            controllers.HealthCheck(responseRecorder, request)            if responseRecorder.Code != tc.statusCode {                t.Errorf("Want status '%d', got '%d'", tc.statusCode, responseRecorder.Code)            }        })    }}
查看完整描述

1 回答

?
开心每一天1111

TA贡献1836条经验 获得超13个赞

我改变了一些东西。


将模板放入Handle函数中,从中取出模板。必须:


func ErrorHandler404(w http.ResponseWriter, r *http.Request) {


    TmplError, _ := template.ParseFiles("./template/layout-erro.html")


    w.WriteHeader(http.StatusNotFound)

    data := models.DetailPageData{

        PageTitle: "Erro 404 - Not Found",

    }

    err := TmplError.Execute(w, data)

    if err != nil {

        log.Println(err)

    }

}


查看完整回答
反对 回复 2023-02-21
  • 1 回答
  • 0 关注
  • 146 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号