stub

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2019 License: MIT Imports: 3 Imported by: 0

README

stub

Build Status codecov GoDoc Go Report Card License Go

stub 让你在 Go 语言单元测试中轻松打桩。

安装

$ go get -u -v github.com/aQuaYi/stub
$

导入

import (
	"github.com/aQuaYi/stub"
)

使用方法

打桩(stub)和模拟(mock)是单元测试中常用的两种技术。Martin Fowler 写了 Mocks Aren't Stubs 来详细说明两者的定义和区别。以下是文章的两个版本的翻译:

对变量打桩
var configFile = "config.json"

func GetConfig() ([]byte, error) {
    return ioutil.ReadFile(configFile)
}

// Test code
stubs := stub.Var(&configFile, "/tmp/test.config")
data, err := GetConfig()
// data will now return contents of the /tmp/test.config file
对函数打桩

函数在 Go 语言中也是一种变量。因此,可以对全局函数进行打桩。

var timeNow = time.Now

func GetDate() int {
    return timeNow().Day()
}

通过 stub 替换 timeNow 函数。

stubs := stub.Var(&timeNow, func() time.Time {
    return time.Date(2015, 6, 1, 0, 0, 0, 0, time.UTC)
})
defer stubs.Restore()

// Test can check that GetDate returns 6

如果替换后的函数,每次都返回固定的结果。还可以使用 Func 替换 Var。

stubs := stub.Func(&timeNow, time.Date(2015, 6, 1, 0, 0, 0, 0, time.UTC))
defer stubs.Restore()

Func 同样也可以对多返回值的函数打桩。

var osHostname = osHostname
// [...] production code using osHostname to call it.

// Test code:
stubs := stub.Func(&osHostname, "fakehost", nil)
defer stubs.Restore()
对环境变量打桩

Env 可以对环境变量打桩,并在事后轻易地恢复。

stubs := stub.Env("GOSTUB_VAR", "test_value")
defer stubs.Restore()
小技巧

Stubs 对象能够被反复使用,并用 Restore 方法一次恢复全部桩。

stubs := stub.Var(&v1, 1)
stubs.Var(&v2, 2)
defer stubs.Restore()

对于简单的打桩,还可以在一行内完成打桩和恢复工作。

defer stub.Var(&v1, 1).Var(&v2, 2).Restore()

同一个 stubs 对象,可以进行多次打桩,并只需要一次 Restore。

stubs := stub.Var(&v1, 1)
defer stubs.Restore()

// Do some testing
stubs.Var(&v1, 5)

// More testing
stubs.Var(&b2, 6)

注意事项

打桩时会记录原始值,并在 Restore 时将其恢复。所以,如果在打桩期间,对变量进行的修改,会丢失。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stubber added in v1.1.0

type Stubber interface {
	// Stub 会把 *originalPtr 变量替换成 fake 变量的内容。
	// NOTICE: 函数在 Go 语言中,也是一种变量。
	Var(originalPtr, fake interface{}) Stubber

	// StubFunc 会把原先 *funcPtr 替换成另一个函数,其具有固定的返回值 returns。
	Func(funcPtr interface{}, returns ...interface{}) Stubber

	// StubEnv 会更改环境变量的值。
	Env(key, value string) Stubber

	// Restore 会把 Stubber 替换过的所有值,全部还原。
	Restore()
}

Stubber 包含了打桩时所需的方法,以及恢复的方法。

func Env added in v1.1.0

func Env(k, v string) Stubber

Env stubs environmental variable

func Func added in v1.1.0

func Func(funcPtr interface{}, returns ...interface{}) Stubber

Func replaces a function variable with a function that returns stubVal. funcVarToStub must be a pointer to a function variable. If the function returns multiple values, then multiple values should be passed to stubFunc. The values must match be assignable to the return values' types.

func Var added in v1.1.0

func Var(original interface{}, fake interface{}) Stubber

Var replaces the *original with fake variable.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL