我有興趣創(chuàng)建一個(gè)httptest.Server簡(jiǎn)單地記錄調(diào)用它的請(qǐng)求。為此,我想使用該github.com/matryer/moq庫(kù)。為了生成一個(gè)模擬http.Handler,我將它的定義復(fù)制到一個(gè)名為的包中client:package clientimport "net/http"type Handler interface { ServeHTTP(http.ResponseWriter, *http.Request)}然后我跑了moq -out handler_mock.go . Handler在client目錄中,它產(chǎn)生了以下內(nèi)容handler_mock.go:// Code generated by moq; DO NOT EDIT.// github.com/matryer/moqpackage clientimport ( "net/http" "sync")var ( lockHandlerMockServeHTTP sync.RWMutex)// Ensure, that HandlerMock does implement Handler.// If this is not the case, regenerate this file with moq.var _ Handler = &HandlerMock{}// HandlerMock is a mock implementation of Handler.//// func TestSomethingThatUsesHandler(t *testing.T) {//// // make and configure a mocked Handler// mockedHandler := &HandlerMock{// ServeHTTPFunc: func(in1 http.ResponseWriter, in2 *http.Request) {// panic("mock out the ServeHTTP method")// },// }//// // use mockedHandler in code that requires Handler// // and then make assertions.//// }type HandlerMock struct { // ServeHTTPFunc mocks the ServeHTTP method. ServeHTTPFunc func(in1 http.ResponseWriter, in2 *http.Request) // calls tracks calls to the methods. calls struct { // ServeHTTP holds details about calls to the ServeHTTP method. ServeHTTP []struct { // In1 is the in1 argument value. In1 http.ResponseWriter // In2 is the in2 argument value. In2 *http.Request } }}以制作具有模擬處理程序函數(shù)的測(cè)試服務(wù)器?
1 回答

慕田峪7331174
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊
錯(cuò)誤消息是說你HandlerMock
沒有實(shí)現(xiàn)接口http.Handler
。但是,*HandlerMock
確實(shí)實(shí)現(xiàn)了它:
func (mock *HandlerMock) ServeHTTP(in1 http.ResponseWriter, in2 *http.Request)
嘗試將語(yǔ)句更改ts := httptest.NewServer(handlerMock)
為ts := httptest.NewServer(&handlerMock)
- 1 回答
- 0 關(guān)注
- 130 瀏覽
添加回答
舉報(bào)
0/150
提交
取消