Gin(golang)Web框架测试结果404,路由正确返回JSON响应
我正在关注文档,当使用curl进行测试时,路由(/services)运行良好,但是集成测试返回404响应。删除/hello的测试并不能解决这个问题,因为我假设是路由器/新记录器出现故障 更新 善良的灵魂在他们的机器上测试了我的代码,测试通过了,因此它必须是本地linuxbrew postgres或Go(均通过linuxbrew安装)问题,错误不会输出到终端 梅因,加油Gin(golang)Web框架测试结果404,路由正确返回JSON响应,go,go-gin,Go,Go Gin,我正在关注文档,当使用curl进行测试时,路由(/services)运行良好,但是集成测试返回404响应。删除/hello的测试并不能解决这个问题,因为我假设是路由器/新记录器出现故障 更新 善良的灵魂在他们的机器上测试了我的代码,测试通过了,因此它必须是本地linuxbrew postgres或Go(均通过linuxbrew安装)问题,错误不会输出到终端 梅因,加油 package main import ( "github.com/gin-gonic/gin"
package main
import (
"github.com/gin-gonic/gin"
"github.com/ramblingbarney/finding_taylor_swift_api/controllers"
"github.com/ramblingbarney/finding_taylor_swift_api/models"
)
func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/hello", func(c *gin.Context) {
c.String(200, "hello world")
})
r.GET("/services", controllers.FindServices)
return r
}
func main() {
r := setupRouter()
models.ConnectDataBase()
r.Run(":8083")
}
web_test.go
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
...
func TestPingRoute(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/hello", nil)
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "hello world", w.Body.String())
}
func TestServicesRoute(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/services", nil)
router.ServeHTTP(w, req)
....
assert.Equal(t, 200, w.Code)
}
对/hello的测试通过