这是一个我试图用一个最小的例子重现的错误,但到目前为止没有成功。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
包含一个MockHandler
实现Handler
接口并使用以下方法生成的结构moq
:
// 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
}
为了生成mock_handler.go
,我最初定义了Handler
inhandler.go
然后在handler
目录中运行命令
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.go
handler_test.go
ensure_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.go
也与简化模块中的完全相同mock_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