0

这是一个我试图用一个最小的例子重现的错误,但到目前为止没有成功。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,我最初定义了Handlerinhandler.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.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.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

4

1 回答 1

0

事实证明,cmd测试失败的是包,因为它无法Handler从包中的测试文件导入接口vpp。所以我将第 17 行更改mock_handler.go为使用 anhttp.Handler而不是 a Handler

var _ http.Handler = &MockHandler{}

现在测试通过了:

~/g/s/g/f/vpp-client> go test ./...
?       github.com/fleetsmith/vpp-client    [no test files]
?       github.com/fleetsmith/vpp-client/cmd    [no test files]
ok      github.com/fleetsmith/vpp-client/vpp    0.462s

由于我现在直接使用标准库中的定义,因此我还能够从中删除Handler接口的定义。ensure_license_test.gohttp

这种方法的缺点是它需要编辑由 自动生成的代码moq,但是,我无法弄清楚如何moq在 Go 的标准库中模拟接口,并且无论如何这个接口都不太可能改变。

于 2020-01-20T21:53:27.997 回答