これは最小限の例で再現を試みたバグですが、これまでのところ成功していません。Go モジュールは次のようになります。
.
├── go.mod
└── handler
├── handler.go
├── handler_test.go
└── mock_handler.go
wherehandler.goは空 (含むpackage handlerのみ) でhandler_test.goあり、Handlerインターフェース定義 (Go の と同じhttp.Handler) とプレースホルダー テストを含みます。
package handler
import (
"net/http"
"testing"
)
type Handler interface {
ServeHTTP(http.ResponseWriter, *http.Request)
}
func TestMockHandler(t *testing.T) {
mockHandler := MockHandler{}
t.Log(mockHandler)
}
インターフェイスを実装し、次を使用して生成さmock_handler.goれる構造体が含まれています。MockHandlerHandlermoq
// Code generated by moq; DO NOT EDIT.
// github.com/matryer/moq
package handler
import (
"net/http"
"sync"
)
var (
lockMockHandlerServeHTTP sync.RWMutex
)
// Ensure, that MockHandler does implement Handler.
// If this is not the case, regenerate this file with moq.
var _ Handler = &MockHandler{}
// MockHandler is a mock implementation of Handler.
//
// func TestSomethingThatUsesHandler(t *testing.T) {
//
// // make and configure a mocked Handler
// mockedHandler := &MockHandler{
// 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 MockHandler 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
}
}
}
// ServeHTTP calls ServeHTTPFunc.
func (mock *MockHandler) ServeHTTP(in1 http.ResponseWriter, in2 *http.Request) {
if mock.ServeHTTPFunc == nil {
panic("MockHandler.ServeHTTPFunc: method is nil but Handler.ServeHTTP was just called")
}
callInfo := struct {
In1 http.ResponseWriter
In2 *http.Request
}{
In1: in1,
In2: in2,
}
lockMockHandlerServeHTTP.Lock()
mock.calls.ServeHTTP = append(mock.calls.ServeHTTP, callInfo)
lockMockHandlerServeHTTP.Unlock()
mock.ServeHTTPFunc(in1, in2)
}
// ServeHTTPCalls gets all the calls that were made to ServeHTTP.
// Check the length with:
// len(mockedHandler.ServeHTTPCalls())
func (mock *MockHandler) ServeHTTPCalls() []struct {
In1 http.ResponseWriter
In2 *http.Request
} {
var calls []struct {
In1 http.ResponseWriter
In2 *http.Request
}
lockMockHandlerServeHTTP.RLock()
calls = mock.calls.ServeHTTP
lockMockHandlerServeHTTP.RUnlock()
return calls
}
を生成するために、最初にinをmock_handler.go定義してから、ディレクトリでコマンドを実行しましたHandlerhandler.gohandler
moq -out mock_handler.go . Handler
その後、テストにのみ使用することを意図しているため、Handlerインターフェイス定義をに移動しました。handler_test.go
go testこの簡単な例では、ルート ディレクトリでパッケージ リスト モードで実行できます。
~/g/s/g/k/mockhandler> go test ./... -count=1
ok github.com/kurtpeek/mockhandler/handler 0.448s
私の「実際の」モジュールには、次のような同様の構造があります。
.
├── cmd
│ └── root.go
├── images
├── main.go
└── vpp
├── ensure_license_test.go
└── mock_handler.go
インターフェイスは、単純化されたモジュールとHandlerまったく同じ方法で定義されます。次のように始まります:ensure_license_test.gohandler_test.goensure_license_test.go
package vpp
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Handler interface {
ServeHTTP(http.ResponseWriter, *http.Request)
}
type MockTestServer struct {
TestServer *httptest.Server
MockHandler *MockHandler
}
mock_handler.gomock_handler.goまた、単純化されたモジュールとまったく同じです (パッケージ名を除く)。
しかしgo test ./...、「実際の」モジュールのルート ディレクトリで実行すると、次のundefinedエラーが発生しますHandler。
~/g/s/g/f/vpp-client> go test ./... -count=1
# github.com/fleetsmith/vpp-client/vpp
vpp/mock_handler.go:17:7: undefined: Handler
ok github.com/fleetsmith/vpp-client/vpp 0.128s
奇妙なことに、vppパッケージ内からこれを実行すると、次のようになります。
> go test ./... -count=1
ok github.com/fleetsmith/vpp-client/vpp 0.601s
最初の例のように、ルート ディレクトリからパッケージ リスト モードで実行したときにgo testの定義を定義できない理由は何でしょうか?Handler