在 Go 测试包中模拟 statsd 客户端

Mock statsd client in Go test package

我使用 this statsd package 将指标发送到我们的 statsd server。为了初始化客户端,我在我的 main 中调用了一个 metrics.Setup() 来执行初始化。这个包看起来像这样:

包:

package metrics

import (
    "fmt"

    "github.com/cactus/go-statsd-client/statsd"
)

// Client can be used to send stats to
var Client StatsdAccess

// Setup initialises metrics gathering
func Setup() {
    if Client == nil {
        prefix := fmt.Sprintf("app.%s", logging.GetHost())
        std, err := statsd.NewBufferedClient(fmt.Sprintf("localhost:1234", prefix, 0, 0)
        if err != nil {
            logrus.Errorf("unable to dial the statsd host: %q", err)
            return
        }
        Client = std
    }
}

// StatsdAccess is used as interface to statsd functions
type StatsdAccess interface {
    Inc(stat string, value int64, rate float32) error
    Gauge(stat string, value int64, rate float32) error
    Timing(stat string, delta int64, rate float32) error
}

从这一刻起,另一个包通过此全局客户端发送指标:metrics.Client.Inc("some.counter", 1, 1.0)。这工作正常,但现在我的测试文件有问题。当包实际使用 metrics package 发送指标时,它们现在会失败。这很明显,因为指标包尚未初始化等。所以我的问题 - 我认为 - 是:如何在我的测试文件中模拟 statsd client

我从事的许多项目都使用 statsd,并且在将调用保留在测试中(因为它们是如此轻量级)和编程到度量接口之间摇摇欲坠(正如您已经完成的 StasdAccess) .

由于接口已经初始化,您应该能够使用该接口来打破代码中的配置依赖性,并提供 test implementation 以在您的测试中使用:

package_metrics/
   testing.go

// testing.go

type StubStatsd struct {}
func (s StubStatsd) Inc(stat string, value int64, rate float32) error {
  return nil
}
func (s StubStatsd) Gauge(...
func (s StubStatsd) Timing(...

现在,当您的测试想要调用方法或初始化需要 StatsdAccess 参数的组件时,它可以使用测试存根来满足要求

func TestSomething_NeedsStatsd(t *testing.T) {
     statsdStub := StubStatsd{}
     someMethodThatRequiresStatsd(stasdStub)
}