Unit testing 用于http处理程序测试的Golang模拟函数
我正在为PostLoginHandler编写一个单元测试,需要模拟会话中间件功能。在我的处理程序中,它调用session.Update(),我想模拟它以返回nil 在阅读了各种答案后,我的第一反应是制作一个SessionManager界面,但即使这样,我也不清楚如何继续 main.go:Unit testing 用于http处理程序测试的Golang模拟函数,unit-testing,go,mocking,Unit Testing,Go,Mocking,我正在为PostLoginHandler编写一个单元测试,需要模拟会话中间件功能。在我的处理程序中,它调用session.Update(),我想模拟它以返回nil 在阅读了各种答案后,我的第一反应是制作一个SessionManager界面,但即使这样,我也不清楚如何继续 main.go: func PostLoginHandler(c web.C, w http.ResponseWriter, r *http.Request) { r.ParseForm() user, pass
func PostLoginHandler(c web.C, w http.ResponseWriter, r *http.Request) {
r.ParseForm()
user, pass := r.PostFormValue("username"), r.PostFormValue("password")
ctx := context.GetContext(c)
if !authorizeUser(user, pass) {
http.Error(w, "Wrong username or password", http.StatusBadRequest)
return
}
ctx.IsLogin = true
err := session.Update(ctx) \\ mock this function call.
if err != nil {
log.Println(err)
return
}
http.Redirect(w, r, "/admin/", http.StatusFound)
}
主要测试:
var loginTests = []struct {
username string
password string
code int
}{
{"admin", "admin", http.StatusFound},
{"", "", http.StatusBadRequest},
{"", "admin", http.StatusBadRequest},
{"admin", "", http.StatusBadRequest},
{"admin", "badpassword", http.StatusBadRequest},
}
func TestPostLoginHandler(t *testing.T) {
// setup()
ctx := &context.Context{IsLogin: false, Data: make(map[string]interface{})}
c := newC()
c.Env["context"] = ctx
for k, test := range loginTests {
v := url.Values{}
v.Set("username", test.username)
v.Set("password", test.password)
r, _ := http.NewRequest("POST", "/login", nil)
r.PostForm = v
resp := httptest.NewRecorder()
m.ServeHTTPC(c, resp, r)
if resp.Code != test.code {
t.Fatalf("TestPostLoginHandler #%v failed. Expected: %v\tReceived: %v", k, test.code, resp.Code)
}
}
}
我向您推荐一对链接: :此链接解释如何使用
mux
package使用响应执行模拟路由
例如:
func TestUsersService_Get_specifiedUser(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/u",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1}`)
}
)
user, _, err := client.Users.Get("u")
if err != nil {
t.Errorf("Users.Get returned error: %v", err)
}
want := &User{ID: Int(1)}
if !reflect.DeepEqual(user, want) {
t.Errorf("Users.Get returned %+v, want %+v",
user, want)
}
}
func TestPostWithForm(t *testing.T) {
testflight.WithServer(Handler(), func(r *testflight.Requester) {
response := r.Post("/post/form", testflight.FORM_ENCODED, "name=Drew")
assert.Equal(t, 201, response.StatusCode)
assert.Equal(t, "Drew created", response.Body)
})
}
:一个用于向服务器发出简单http请求的包,使用testflight和mux
,对于make mock端点,您可以进行完美的测试
例如:
func TestUsersService_Get_specifiedUser(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/u",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1}`)
}
)
user, _, err := client.Users.Get("u")
if err != nil {
t.Errorf("Users.Get returned error: %v", err)
}
want := &User{ID: Int(1)}
if !reflect.DeepEqual(user, want) {
t.Errorf("Users.Get returned %+v, want %+v",
user, want)
}
}
func TestPostWithForm(t *testing.T) {
testflight.WithServer(Handler(), func(r *testflight.Requester) {
response := r.Post("/post/form", testflight.FORM_ENCODED, "name=Drew")
assert.Equal(t, 201, response.StatusCode)
assert.Equal(t, "Drew created", response.Body)
})
}
正如您所建议的,在这里,接口将是一个合适的解决方案。您只是不确定如何定义接口吗?接口是一个很好的解决方案。另一种是使用sqlite内存数据库。如果好奇的话,我已经在本回购协议中这样做了。