第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用 Go 的 moq 模擬 http.Handler?

如何使用 Go 的 moq 模擬 http.Handler?

Go
Cats萌萌 2022-05-05 17:56:22
我有興趣創(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)


查看完整回答
反對(duì) 回復(fù) 2022-05-05
  • 1 回答
  • 0 關(guān)注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)